美文网首页
QFileSystemModel-简单的文件浏览器

QFileSystemModel-简单的文件浏览器

作者: 静候那一米阳光 | 来源:发表于2017-12-22 00:57 被阅读0次

    QFileSystemModel a simple file explorer

    效果图

    【dialog.h】

    #ifndef DIALOG_H
    #define DIALOG_H
    
    #include <QDialog>
    #include <QFileSystemModel>
    namespace Ui {
    class Dialog;
    }
    
    class Dialog : public QDialog
    {
        Q_OBJECT
    
    public:
        explicit Dialog(QWidget *parent = 0);
        ~Dialog();
    
    private slots:
        void on_treeView_clicked(const QModelIndex &index);
    
    private:
        Ui::Dialog *ui;
        QFileSystemModel *dirmodel;
        QFileSystemModel *filemodel;
    };
    
    #endif // DIALOG_H
    

    【dialog.cpp】

    #include "dialog.h"
    #include "ui_dialog.h"
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
        QString sPath = "/Users/zdy/Desktop";
        dirmodel = new QFileSystemModel(this);
        dirmodel->setFilter(QDir::NoDotAndDotDot|QDir::AllDirs);
        dirmodel->setRootPath(sPath);
        ui->treeView->setModel(dirmodel);
    
        filemodel = new QFileSystemModel(this);
        filemodel->setRootPath(sPath);
        ui->listView->setModel(filemodel);
    
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    void Dialog::on_treeView_clicked(const QModelIndex &index)
    {
    
        QString sPath = dirmodel->fileInfo(index).absoluteFilePath();
        ui->listView->setRootIndex(filemodel->setRootPath(sPath));
    }
    

    相关文章

      网友评论

          本文标题:QFileSystemModel-简单的文件浏览器

          本文链接:https://www.haomeiwen.com/subject/odozwxtx.html