Qt对QTreeView和QFileSystemModel的使用,文件路径返回各种值

相关资料:

https://stackoverflow.com/questions/14962902/qt-adding-own-column-to-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 
isDir:%2 
filePath:%3 
type:%4 
fileName:%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

PS:自定义文件管理类,增加一列

 1 class yourSystemModel : public QFileSystemModel
 2 {
 3     Q_OBJECT
 4 
 5     int columnCount(const QModelIndex& parent = QModelIndex()) const
 6     {
 7         return QFileSystemModel::columnCount()+1;
 8     }
 9 
10     QVariant data(const QModelIndex& index,int role) const
11     {
12        if(!index.isValid()){return QFileSystemModel::data(index,role);}
13        if(index.column()==columnCount()-1)
14        {
15            switch(role)
16            {
17               case(Qt::DisplayRole):
18                   {return fileInfo(index).fileName();}
19               case(Qt::TextAlignmentRole):
20                   {return Qt::AlignHCenter;}
21               default:{}
22            }
23        }
24        return QFileSystemModel::data(index,role);
25    }
26 };

PS:修改表头名称显示

1 QStringList headers;
2     headers << QString::fromLocal8Bit("文件") << QString::fromLocal8Bit("创建日期")
3             << QString::fromLocal8Bit("修改日期") << QString::fromLocal8Bit("文件版本")
4             << QString::fromLocal8Bit("战场名称");;
5 
6     QStandardItemModel *itemModel =new QStandardItemModel();
7         itemModel->setHorizontalHeaderLabels(headers);
8         ui->treeView->header()->setModel(itemModel);

PS:更多文件路径操作

    QString file_full;

    QFileInfo fi;

    file_full = QFileDialog::getOpenFileName(this);

    fi = QFileInfo(file_full);

    qDebug() << fi.filePath();

    qDebug() << fi.absoluteFilePath();

    qDebug() << fi.canonicalFilePath();

    qDebug() << fi.fileName();

    qDebug() << fi.baseName();

    qDebug() << fi.completeBaseName();

    qDebug() << fi.suffix();

    qDebug() << fi.bundleName();

    qDebug() << fi.completeSuffix();

    qDebug() << fi.path();

    qDebug() << fi.absolutePath();

    qDebug() << fi.canonicalPath();

    qDebug() << fi.symLinkTarget();

    qDebug() << fi.owner();

    qDebug() << fi.group();

    qDebug() << "----------------";

    qDebug() << fi.dir().path();

    qDebug() << fi.dir().absolutePath();

    qDebug() << fi.dir().canonicalPath();

    qDebug() << fi.dir().dirName();

    qDebug() << fi.dir().dirName();

    qDebug() << fi.dir().currentPath();

    qDebug() << fi.dir().homePath();

    qDebug() << fi.dir().rootPath();

    qDebug() << fi.dir().tempPath();

 

作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

欢迎关注我,一起进步!扫描下方二维码即可加我

原文地址:https://www.cnblogs.com/FKdelphi/p/13540179.html