1. 程式人生 > 實用技巧 >Qt對QTreeView和QFileSystemModel的使用

Qt對QTreeView和QFileSystemModel的使用

.pro

 1 QT       += core gui
 2 
 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 4 
 5 CONFIG += c++11
 6 
 7 # The following define makes your compiler emit warnings if you use
 8 # any Qt feature that has been marked deprecated (the exact warnings
 9 # depend on your compiler). Please consult the documentation of the
10 # deprecated API in order to know how to port your code away from it. 11 DEFINES += QT_DEPRECATED_WARNINGS 12 13 # You can also make your code fail to compile if it uses deprecated APIs. 14 # In order to do so, uncomment the following line. 15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 17 18 SOURCES += \ 19 main.cpp \ 20 mainwindow.cpp 21 22 HEADERS += \ 23 mainwindow.h 24 25 FORMS += \ 26 mainwindow.ui 27 28 TARGET = ../../QtTreeView/bin/QtTreeView 29 30 # Default rules for
deployment. 31 qnx: target.path = /tmp/$${TARGET}/bin 32 else: unix:!android: target.path = /opt/$${TARGET}/bin 33 !isEmpty(target.path): INSTALLS += target
View Code

mainwindow.h

 1 #ifndef MAINWINDOW_H
 2 #define MAINWINDOW_H
 3 
 4 #include <QMainWindow>
 5 #include <QStandardItemModel>
 6 #include <QDebug>
 7 #include <QMessageBox>
 8 #include <QFileSystemModel>
 9 
10 QT_BEGIN_NAMESPACE
11 namespace Ui { class MainWindow; }
12 QT_END_NAMESPACE
13 
14 class MainWindow : public QMainWindow
15 {
16     Q_OBJECT
17 
18 public:
19     MainWindow(QWidget *parent = nullptr);
20     ~MainWindow();
21 
22 private slots:
23     void on_treeView_clicked(const QModelIndex &index);
24 
25 private:
26     Ui::MainWindow *ui;
27     QStandardItemModel *m_pModel;
28     QFileSystemModel *m_pFileModel;
29 };
30 #endif // MAINWINDOW_H
View Code

mainwindow.cpp

 1 #include "mainwindow.h"
 2 #include "ui_mainwindow.h"
 3 
 4 MainWindow::MainWindow(QWidget *parent)
 5     : QMainWindow(parent)
 6     , ui(new Ui::MainWindow)
 7 {
 8     ui->setupUi(this);
 9     setWindowTitle(QStringLiteral("QTreeView和QFileSystemModel的使用"));
10 
11     // 隱藏樹的頭顯示用
12     ui->treeView->header()->hide();
13     // 設定為不可編輯
14     ui->treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
15     // 建立一個檔案系統模型
16     m_pFileModel = new QFileSystemModel(this);
17     m_pFileModel->setRootPath(QCoreApplication::applicationDirPath() + "/XML");
18     QStringList sNameFilter;
19     sNameFilter << "*.txt";
20     m_pFileModel->setNameFilterDisables(false);
21     m_pFileModel->setNameFilters(sNameFilter);
22     ui->treeView->setModel(m_pFileModel);
23     ui->treeView->setRootIndex(m_pFileModel->index(QCoreApplication::applicationDirPath() + "/XML"));
24     // 隱藏後面三列
25     ui->treeView->setColumnHidden(1, true);
26     ui->treeView->setColumnHidden(2, true);
27     ui->treeView->setColumnHidden(3, true);
28 }
29 
30 MainWindow::~MainWindow()
31 {
32     delete ui;
33 }
34 
35 void MainWindow::on_treeView_clicked(const QModelIndex &index)
36 {
37     QString sData;
38     QString sDir;
39     QString sfilePath;
40     QString stype;
41     QString sfileName;
42     QString sFromatStr = QStringLiteral("data:%1 \nisDir:%2 \nfilePath:%3 \ntype:%4 \nfileName:%5");
43     sData = index.data().toString();
44     sDir = QString::number(m_pFileModel->isDir(index));
45     sfilePath = m_pFileModel->filePath(index);
46     stype = m_pFileModel->type(index);
47     sfileName = m_pFileModel->fileName(index);
48 
49     ui->textEdit->setText("");
50     ui->textEdit->append(sFromatStr.arg(sData).arg(sDir).arg(sfilePath).arg(stype).arg(sfileName));
51 }
View Code