From deff6bad4342da26379dd7facb49eda3856fcdeb Mon Sep 17 00:00:00 2001 From: touwoyimuli Date: Sun, 15 Sep 2019 00:35:08 +0800 Subject: [PATCH] feat: QFileSystemModel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QFileSystemModel(文件系统的模型)的介绍和使用 --- QtQFileSystemModelEx/ExQFileSystemModel.cpp | 68 ++++++++++++++ QtQFileSystemModelEx/ExQFileSystemModel.h | 37 ++++++++ QtQFileSystemModelEx/ExQFileSystemModel.ui | 85 ++++++++++++++++++ QtQFileSystemModelEx/QtQFileSystemModelEx.pro | 42 +++++++++ QtQFileSystemModelEx/main.cpp | 11 +++ QtQFileSystemModelEx/qt.ico | Bin 0 -> 4286 bytes QtQStringListModelEx/ExQStringListModel.cpp | 14 +++ QtQStringListModelEx/ExQStringListModel.h | 22 +++++ QtQStringListModelEx/ExQStringListModel.ui | 20 +++++ QtQStringListModelEx/QtQStringListModelEx.pro | 40 +++++++++ QtQStringListModelEx/main.cpp | 11 +++ 11 files changed, 350 insertions(+) create mode 100644 QtQFileSystemModelEx/ExQFileSystemModel.cpp create mode 100644 QtQFileSystemModelEx/ExQFileSystemModel.h create mode 100644 QtQFileSystemModelEx/ExQFileSystemModel.ui create mode 100644 QtQFileSystemModelEx/QtQFileSystemModelEx.pro create mode 100644 QtQFileSystemModelEx/main.cpp create mode 100644 QtQFileSystemModelEx/qt.ico create mode 100644 QtQStringListModelEx/ExQStringListModel.cpp create mode 100644 QtQStringListModelEx/ExQStringListModel.h create mode 100644 QtQStringListModelEx/ExQStringListModel.ui create mode 100644 QtQStringListModelEx/QtQStringListModelEx.pro create mode 100644 QtQStringListModelEx/main.cpp diff --git a/QtQFileSystemModelEx/ExQFileSystemModel.cpp b/QtQFileSystemModelEx/ExQFileSystemModel.cpp new file mode 100644 index 0000000..87c3c5d --- /dev/null +++ b/QtQFileSystemModelEx/ExQFileSystemModel.cpp @@ -0,0 +1,68 @@ +#include "ExQFileSystemModel.h" +#include "ui_ExQFileSystemModel.h" + +ExQFileSystemModel::ExQFileSystemModel(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::ExQFileSystemModel) +{ + ui->setupUi(this); + setWindowTitle(QObject::tr("文件系统模型QFileSystemModel的介绍和使用")); + + init(); + connect(ui->treeView, &QTreeView::clicked, ui->listView, &QListView::setRootIndex); + connect(ui->treeView, &QTreeView::clicked, ui->tableView, &QTableView::setRootIndex); +} + +ExQFileSystemModel::~ExQFileSystemModel() +{ + delete ui; +} + +//初始化,以及初始化状态栏 +void ExQFileSystemModel::init() +{ + //设置数据模型,且加载到各个视图上面 + m_model = new QFileSystemModel(this); + QString currPath = QDir::currentPath(); //获取当前路径 + m_model->setRootPath(currPath); //设置根目录 + ui->treeView->setModel(m_model); //设置数据模型 + ui->listView->setModel(m_model); + ui->tableView->setModel(m_model); + ui->columnView->setModel(m_model); + + //初始化状态栏 + m_labFileName = new QLabel("名称:", ui->statusBar); + m_labFileName->setMinimumWidth(180); + m_labFileSize = new QLabel("大小:", ui->statusBar); + m_labFileSize->setFixedWidth(130); + m_labFileType = new QLabel("类型:", ui->statusBar); + m_labFileType->setFixedWidth(130); + m_labPath = new QLabel("路径:" + currPath, ui->statusBar); + m_chkBoxIsFile = new QCheckBox("当前为文件夹", ui->statusBar); + m_chkBoxIsFile->setFixedWidth(130); + + //各种QLable添加到状态栏 + ui->statusBar->addWidget(m_labFileName); + ui->statusBar->addWidget(m_labFileSize); + ui->statusBar->addWidget(m_labFileType); + ui->statusBar->addWidget(m_chkBoxIsFile); + ui->statusBar->addWidget(m_labPath); +} + +//单击treeView,会在状态栏显示当前节点的信息 +void ExQFileSystemModel::on_treeView_clicked(const QModelIndex &index) +{ + m_chkBoxIsFile->setChecked(m_model->isDir(index)); //是否是目录 + m_labFileName->setText("名称:" + m_model->fileName(index)); //文件名称 + double size = m_model->size(index) / 1024.0; + + if (size < 1024) + m_labFileSize->setText("类型:" + QString::number(size, 'f', 2) + "KB"); + else if (1024 <= size && size < 1024 * 1024) + m_labFileSize->setText("类型:" + QString::number(size / 1024, 'f', 2) + "MB"); + else + m_labFileSize->setText("类型:" + QString::number(size / (1024 * 1024), 'f', 2) + "GB"); + + m_labFileType->setText(m_model->type(index)); + m_labPath->setText(m_model->filePath(index)); +} diff --git a/QtQFileSystemModelEx/ExQFileSystemModel.h b/QtQFileSystemModelEx/ExQFileSystemModel.h new file mode 100644 index 0000000..ab22ab4 --- /dev/null +++ b/QtQFileSystemModelEx/ExQFileSystemModel.h @@ -0,0 +1,37 @@ +#ifndef EXQFILESYSTEMMODEL_H +#define EXQFILESYSTEMMODEL_H + +#include +#include +#include +#include + +namespace Ui { +class ExQFileSystemModel; +} + +class ExQFileSystemModel : public QMainWindow +{ + Q_OBJECT + +public: + explicit ExQFileSystemModel(QWidget *parent = nullptr); + ~ExQFileSystemModel(); + + void init(); //初始化,以及初始化状态栏 + +private slots: + void on_treeView_clicked(const QModelIndex &index); //单击treeView,会在状态栏显示当前节点的信息 + +private: + Ui::ExQFileSystemModel *ui; + + QLabel* m_labFileName; //文件名 + QLabel* m_labFileSize; //文件大小 + QLabel* m_labFileType; //文件类型 + QLabel* m_labPath; //路径 + QCheckBox* m_chkBoxIsFile; //当前是否为文件或文件夹 + QFileSystemModel* m_model; //设置文件系统的模型 +}; + +#endif // EXQFILESYSTEMMODEL_H diff --git a/QtQFileSystemModelEx/ExQFileSystemModel.ui b/QtQFileSystemModelEx/ExQFileSystemModel.ui new file mode 100644 index 0000000..24ebfed --- /dev/null +++ b/QtQFileSystemModelEx/ExQFileSystemModel.ui @@ -0,0 +1,85 @@ + + + ExQFileSystemModel + + + + 0 + 0 + 1414 + 786 + + + + ExQFileSystemModel + + + + + + + Qt::Horizontal + + + + Tree View: + + + + + + + + + + Qt::Vertical + + + + + + + List View: + + + + + + + + + + + + Table View: + + + + + + + + + + + + + Column View: + + + + + + + + + + + + + + + + + + diff --git a/QtQFileSystemModelEx/QtQFileSystemModelEx.pro b/QtQFileSystemModelEx/QtQFileSystemModelEx.pro new file mode 100644 index 0000000..d6c8b1d --- /dev/null +++ b/QtQFileSystemModelEx/QtQFileSystemModelEx.pro @@ -0,0 +1,42 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2019-09-14T16:56:30 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = QtQFileSystemModelEx +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +CONFIG += c++11 + +SOURCES += \ + main.cpp \ + ExQFileSystemModel.cpp + +HEADERS += \ + ExQFileSystemModel.h + +FORMS += \ + ExQFileSystemModel.ui + +RC_ICONS += qt.ico + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/QtQFileSystemModelEx/main.cpp b/QtQFileSystemModelEx/main.cpp new file mode 100644 index 0000000..a7c907d --- /dev/null +++ b/QtQFileSystemModelEx/main.cpp @@ -0,0 +1,11 @@ +#include "ExQFileSystemModel.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + ExQFileSystemModel w; + w.show(); + + return a.exec(); +} diff --git a/QtQFileSystemModelEx/qt.ico b/QtQFileSystemModelEx/qt.ico new file mode 100644 index 0000000000000000000000000000000000000000..0ee0568945377c30d5d973d89afcc31770cd8818 GIT binary patch literal 4286 zcmeHKdr(y86+io87ZyYwA_hySrjx{)Njo~7w3D=Go0O&*)2xbtP|_lTj{v^F6l<|4 z!TRE16coi*F_>DPiHV~$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9s zmJPw2riLY7$2mm8UOd{F!`U=%RLoYQD_8DZdu4oOyUby6jc`@fuID_~6l-uJ_o#~Iht=`Vut!w3I zPT_Mj-Q_3xO1Q*a5|5R zkfGrmqMbSP?D!t8+f8UcsDqW)pCy~mj5+eJ_;~tzmaE_N$uh0b);FzCqdt+)!t0S&hcxBwX#Ng~Q>%HJb@7ISO9WuD-Ws zYw#eb%NkM8v`TBS-a4r_U zZwHP%j$Vr%1NIhJi=IR?uOS1U3nJHp;$lv~-b=aKZtEIvo_}xD@V;-(41#qXpTirK z=q>T%?|@)9j)vmqiAQjY;dr0F1>Oxo-wuCRwuHgDX*^n2kAZ%+3YJ`rlSksSxOt`e zaB0d3JYiYq+h<+Jd&dnU_pu>!0=oCS0Nb`FU@IDh?jl~7TYb>G%?CZjK78i}z*amC zUAw37-SZ-97Wl!sc8Ejb6QxuZ`K7v~YuONd%(^LTxixQeZe@xV5SUH(7$yHwU9E_`fj_S-voQd_h?I-zHrnpw?2gij z-{EgC0{=xY#Za*^hECprSzMPO*`&Ise3j1C#?pU`vAp+V`TP@Qj9B)BThF@M7+NdF zeV9*ulDwM1catZ?yUJY`?;tLlljN7h{yxmuB3Da>s;D`G&#eD}mJH?ThaFqwtIZ;> z_H5PDEm^7`+z-vm$p8Nl|69&)<_KWevy%75gMfQ0?xEpe)zGt&?Wg%a9&lRi!mwam F{Vzx7jH3Vm literal 0 HcmV?d00001 diff --git a/QtQStringListModelEx/ExQStringListModel.cpp b/QtQStringListModelEx/ExQStringListModel.cpp new file mode 100644 index 0000000..6c7ec1b --- /dev/null +++ b/QtQStringListModelEx/ExQStringListModel.cpp @@ -0,0 +1,14 @@ +#include "ExQStringListModel.h" +#include "ui_ExQStringListModel.h" + +ExQStringListModel::ExQStringListModel(QWidget *parent) : + QWidget(parent), + ui(new Ui::ExQStringListModel) +{ + ui->setupUi(this); +} + +ExQStringListModel::~ExQStringListModel() +{ + delete ui; +} diff --git a/QtQStringListModelEx/ExQStringListModel.h b/QtQStringListModelEx/ExQStringListModel.h new file mode 100644 index 0000000..d6c5079 --- /dev/null +++ b/QtQStringListModelEx/ExQStringListModel.h @@ -0,0 +1,22 @@ +#ifndef EXQSTRINGLISTMODEL_H +#define EXQSTRINGLISTMODEL_H + +#include + +namespace Ui { +class ExQStringListModel; +} + +class ExQStringListModel : public QWidget +{ + Q_OBJECT + +public: + explicit ExQStringListModel(QWidget *parent = nullptr); + ~ExQStringListModel(); + +private: + Ui::ExQStringListModel *ui; +}; + +#endif // EXQSTRINGLISTMODEL_H diff --git a/QtQStringListModelEx/ExQStringListModel.ui b/QtQStringListModelEx/ExQStringListModel.ui new file mode 100644 index 0000000..334ae1c --- /dev/null +++ b/QtQStringListModelEx/ExQStringListModel.ui @@ -0,0 +1,20 @@ + + ExQStringListModel + + + + 0 + 0 + 400 + 300 + + + + ExQStringListModel + + + + + + + diff --git a/QtQStringListModelEx/QtQStringListModelEx.pro b/QtQStringListModelEx/QtQStringListModelEx.pro new file mode 100644 index 0000000..b642072 --- /dev/null +++ b/QtQStringListModelEx/QtQStringListModelEx.pro @@ -0,0 +1,40 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2019-09-14T21:36:41 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = QtQStringListModelEx +TEMPLATE = app + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +CONFIG += c++11 + +SOURCES += \ + main.cpp \ + ExQStringListModel.cpp + +HEADERS += \ + ExQStringListModel.h + +FORMS += \ + ExQStringListModel.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/QtQStringListModelEx/main.cpp b/QtQStringListModelEx/main.cpp new file mode 100644 index 0000000..326cd8f --- /dev/null +++ b/QtQStringListModelEx/main.cpp @@ -0,0 +1,11 @@ +#include "ExQStringListModel.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + ExQStringListModel w; + w.show(); + + return a.exec(); +}