feat: QFileSystemModel
QFileSystemModel(文件系统的模型)的介绍和使用
This commit is contained in:
parent
f070d975d5
commit
deff6bad43
68
QtQFileSystemModelEx/ExQFileSystemModel.cpp
Normal file
68
QtQFileSystemModelEx/ExQFileSystemModel.cpp
Normal file
@ -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));
|
||||||
|
}
|
37
QtQFileSystemModelEx/ExQFileSystemModel.h
Normal file
37
QtQFileSystemModelEx/ExQFileSystemModel.h
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#ifndef EXQFILESYSTEMMODEL_H
|
||||||
|
#define EXQFILESYSTEMMODEL_H
|
||||||
|
|
||||||
|
#include <QMainWindow>
|
||||||
|
#include <QLabel>
|
||||||
|
#include <QCheckBox>
|
||||||
|
#include <QFileSystemModel>
|
||||||
|
|
||||||
|
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
|
85
QtQFileSystemModelEx/ExQFileSystemModel.ui
Normal file
85
QtQFileSystemModelEx/ExQFileSystemModel.ui
Normal file
@ -0,0 +1,85 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<ui version="4.0">
|
||||||
|
<class>ExQFileSystemModel</class>
|
||||||
|
<widget class="QMainWindow" name="ExQFileSystemModel">
|
||||||
|
<property name="geometry">
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>1414</width>
|
||||||
|
<height>786</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="windowTitle">
|
||||||
|
<string>ExQFileSystemModel</string>
|
||||||
|
</property>
|
||||||
|
<widget class="QWidget" name="centralWidget">
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout_6">
|
||||||
|
<item>
|
||||||
|
<widget class="QSplitter" name="splitter_2">
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Horizontal</enum>
|
||||||
|
</property>
|
||||||
|
<widget class="QGroupBox" name="groupBox">
|
||||||
|
<property name="title">
|
||||||
|
<string>Tree View:</string>
|
||||||
|
</property>
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||||
|
<item>
|
||||||
|
<widget class="QTreeView" name="treeView"/>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
<widget class="QSplitter" name="splitter">
|
||||||
|
<property name="orientation">
|
||||||
|
<enum>Qt::Vertical</enum>
|
||||||
|
</property>
|
||||||
|
<widget class="QWidget" name="">
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout_5">
|
||||||
|
<item>
|
||||||
|
<widget class="QGroupBox" name="groupBox_2">
|
||||||
|
<property name="title">
|
||||||
|
<string>List View:</string>
|
||||||
|
</property>
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||||
|
<item>
|
||||||
|
<widget class="QListView" name="listView"/>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QGroupBox" name="groupBox_3">
|
||||||
|
<property name="title">
|
||||||
|
<string>Table View:</string>
|
||||||
|
</property>
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout_3">
|
||||||
|
<item>
|
||||||
|
<widget class="QTableView" name="tableView"/>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
<widget class="QGroupBox" name="groupBox_4">
|
||||||
|
<property name="title">
|
||||||
|
<string>Column View:</string>
|
||||||
|
</property>
|
||||||
|
<layout class="QHBoxLayout" name="horizontalLayout_4">
|
||||||
|
<item>
|
||||||
|
<widget class="QColumnView" name="columnView"/>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
</widget>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
|
</widget>
|
||||||
|
<widget class="QStatusBar" name="statusBar"/>
|
||||||
|
</widget>
|
||||||
|
<layoutdefault spacing="6" margin="11"/>
|
||||||
|
<resources/>
|
||||||
|
<connections/>
|
||||||
|
</ui>
|
42
QtQFileSystemModelEx/QtQFileSystemModelEx.pro
Normal file
42
QtQFileSystemModelEx/QtQFileSystemModelEx.pro
Normal file
@ -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
|
11
QtQFileSystemModelEx/main.cpp
Normal file
11
QtQFileSystemModelEx/main.cpp
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include "ExQFileSystemModel.h"
|
||||||
|
#include <QApplication>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
QApplication a(argc, argv);
|
||||||
|
ExQFileSystemModel w;
|
||||||
|
w.show();
|
||||||
|
|
||||||
|
return a.exec();
|
||||||
|
}
|
BIN
QtQFileSystemModelEx/qt.ico
Normal file
BIN
QtQFileSystemModelEx/qt.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.2 KiB |
14
QtQStringListModelEx/ExQStringListModel.cpp
Normal file
14
QtQStringListModelEx/ExQStringListModel.cpp
Normal file
@ -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;
|
||||||
|
}
|
22
QtQStringListModelEx/ExQStringListModel.h
Normal file
22
QtQStringListModelEx/ExQStringListModel.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
#ifndef EXQSTRINGLISTMODEL_H
|
||||||
|
#define EXQSTRINGLISTMODEL_H
|
||||||
|
|
||||||
|
#include <QWidget>
|
||||||
|
|
||||||
|
namespace Ui {
|
||||||
|
class ExQStringListModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
class ExQStringListModel : public QWidget
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit ExQStringListModel(QWidget *parent = nullptr);
|
||||||
|
~ExQStringListModel();
|
||||||
|
|
||||||
|
private:
|
||||||
|
Ui::ExQStringListModel *ui;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // EXQSTRINGLISTMODEL_H
|
20
QtQStringListModelEx/ExQStringListModel.ui
Normal file
20
QtQStringListModelEx/ExQStringListModel.ui
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<ui version="4.0">
|
||||||
|
<class>ExQStringListModel</class>
|
||||||
|
<widget class="QWidget" name="ExQStringListModel" >
|
||||||
|
<property name="geometry" >
|
||||||
|
<rect>
|
||||||
|
<x>0</x>
|
||||||
|
<y>0</y>
|
||||||
|
<width>400</width>
|
||||||
|
<height>300</height>
|
||||||
|
</rect>
|
||||||
|
</property>
|
||||||
|
<property name="windowTitle" >
|
||||||
|
<string>ExQStringListModel</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
<layoutDefault spacing="6" margin="11" />
|
||||||
|
<pixmapfunction></pixmapfunction>
|
||||||
|
<resources/>
|
||||||
|
<connections/>
|
||||||
|
</ui>
|
40
QtQStringListModelEx/QtQStringListModelEx.pro
Normal file
40
QtQStringListModelEx/QtQStringListModelEx.pro
Normal file
@ -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
|
11
QtQStringListModelEx/main.cpp
Normal file
11
QtQStringListModelEx/main.cpp
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include "ExQStringListModel.h"
|
||||||
|
#include <QApplication>
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
QApplication a(argc, argv);
|
||||||
|
ExQStringListModel w;
|
||||||
|
w.show();
|
||||||
|
|
||||||
|
return a.exec();
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user