zoukankan      html  css  js  c++  java
  • Qt QFileSystemModel QDirModel 示例代码, 使用方法

    1.  QFileSystemModel 查看,添加 和 删除目录

    2. 实现代码

    dialog.h

    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include <QDialog>
    #include <QtGui>
    
    class Dialog : public QDialog
    {
        Q_OBJECT
        
    public:
        Dialog(QWidget *parent = 0);
    
    private slots:
        void createDirectory();
        void remove();
    
    private:
        QFileSystemModel *model;
        QTreeView *treeView;
    
    };
    
    #endif // DIALOG_H
    

    dialog.cpp

    #include "dialog.h"
    
    Dialog::Dialog(QWidget *parent)
        : QDialog(parent)
    {
        setWindowTitle("QFileSystemModel");
        model = new QFileSystemModel;
        model->setReadOnly(false);
        model->setRootPath(QDir::currentPath());
    
        treeView = new QTreeView;
        treeView->setModel(model);
    
        treeView->header()->setStretchLastSection(true);
        treeView->header()->setSortIndicator(0, Qt::AscendingOrder);
        treeView->header()->setSortIndicatorShown(true);
        treeView->header()->setClickable(true);
    
        QModelIndex index = model->index(QDir::currentPath());
        treeView->expand(index);
        treeView->scrollTo(index);
        treeView->resizeColumnToContents(0);
    
        QPushButton *createButton = new QPushButton(tr("Create Dir"));
        QPushButton *removeButton = new QPushButton(tr("Remove Dir"));
        connect(createButton, SIGNAL(clicked()), this, SLOT(createDirectory()));
        connect(removeButton, SIGNAL(clicked()), this, SLOT(remove()));
    
        QHBoxLayout *hLayout = new QHBoxLayout;
        hLayout->addWidget(createButton);
        hLayout->addWidget(removeButton);
    
        QVBoxLayout *vLayout = new QVBoxLayout;
        vLayout->addWidget(treeView);
        vLayout->addLayout(hLayout);
    
        setLayout(vLayout);
    }
    
    void Dialog::createDirectory()
    {
        QModelIndex index = treeView->currentIndex();
        if( !index.isValid() )
            return;
        QString dirName = QInputDialog::getText(this, tr("create Dir"), tr("Dir name"));
        if( !dirName.isEmpty() )
        {
            if( !model->mkdir(index, dirName).isValid() )
                QMessageBox::information(this, tr("Create Dir"), tr("Failed to create Dir"));
    
        }
    }
    
    void Dialog::remove()
    {
        QModelIndex index = treeView->currentIndex();
        if( !index.isValid() )
            return;
        bool ok;
        if( model->fileInfo(index).isDir() )
            ok = model->rmdir(index);
        else
            ok = model->remove(index);
    
        if(!ok)
            QMessageBox::information(this, tr("Remove"), tr("Failed to remove Dir").arg(model->fileName(index)));
    }
    


    main.cpp

    #include "dialog.h"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Dialog w;
        w.setSizeIncrement(400,300);
        w.show();
        
        return a.exec();
    }
    


     

  • 相关阅读:
    WCF基础教程之开篇:创建、测试和调用WCF
    程序员必须知道的几个Git代码托管平台
    2015继续任性——不会Git命令,照样玩转Git
    何必苦等VS2015?来看看VS2013下实现移动端的跨平台开发
    Android Studio 1.0.2项目实战——从一个APP的开发过程认识Android Studio
    (转)创建Graphics的三种方法
    sql自增长列重新从1计算
    IT之人生感悟
    c# 高效率导出多维表头excel
    SQL 之witn as语法
  • 原文地址:https://www.cnblogs.com/xj626852095/p/3648210.html
Copyright © 2011-2022 走看看