在这个案例中,借助QFileSystemModel类的对象,表示计算机磁盘上的文件系统,将其关联于QTreeView、QTableView和QListView三种不同的视图。不难发现,即便是同一个模型,当从不同视图看过去时,亦呈现出不同的逻辑结构。通过QTreeView看到的模型呈树状结构,通过QTableView看到的模型呈表格结构,而通过QListView看到的模型则呈列表结构。这为将同一个数据源展现为不同的外观,提供了可能。
通过QtCreator,在C:\Users\Minwei\Projects\Qt路径下,创建名为FileSystem的项目。
C:\Users\Minwei\Projects\Qt\FileSystem\filesystemdialog.ui:
xxxxxxxxxx
1651
2<ui version="4.0">
3 <class>FileSystemDialog</class>
4 <widget class="QDialog" name="FileSystemDialog">
5 <property name="geometry">
6 <rect>
7 <x>0</x>
8 <y>0</y>
9 <width>800</width>
10 <height>600</height>
11 </rect>
12 </property>
13 <property name="windowTitle">
14 <string>文件系统</string>
15 </property>
16 <layout class="QVBoxLayout" name="m_layoutVer">
17 <item>
18 <widget class="QSplitter" name="m_splitterHor">
19 <property name="sizePolicy">
20 <sizepolicy hsizetype="Expanding" vsizetype="Expanding">
21 <horstretch>0</horstretch>
22 <verstretch>0</verstretch>
23 </sizepolicy>
24 </property>
25 <property name="orientation">
26 <enum>Qt::Horizontal</enum>
27 </property>
28 <widget class="QSplitter" name="m_splitterVer">
29 <property name="orientation">
30 <enum>Qt::Vertical</enum>
31 </property>
32 <widget class="QTreeView" name="m_tree">
33 <property name="frameShape">
34 <enum>QFrame::WinPanel</enum>
35 </property>
36 </widget>
37 <widget class="QTableView" name="m_table">
38 <property name="frameShape">
39 <enum>QFrame::WinPanel</enum>
40 </property>
41 </widget>
42 </widget>
43 <widget class="QListView" name="m_list">
44 <property name="frameShape">
45 <enum>QFrame::WinPanel</enum>
46 </property>
47 </widget>
48 </widget>
49 </item>
50 <item>
51 <layout class="QGridLayout" name="m_layoutGrid">
52 <property name="spacing">
53 <number>5</number>
54 </property>
55 <item row="0" column="0">
56 <widget class="QLabel" name="m_labFileName">
57 <property name="frameShape">
58 <enum>QFrame::WinPanel</enum>
59 </property>
60 <property name="frameShadow">
61 <enum>QFrame::Sunken</enum>
62 </property>
63 <property name="text">
64 <string/>
65 </property>
66 </widget>
67 </item>
68 <item row="0" column="1">
69 <widget class="QLabel" name="m_labFileSize">
70 <property name="frameShape">
71 <enum>QFrame::WinPanel</enum>
72 </property>
73 <property name="frameShadow">
74 <enum>QFrame::Sunken</enum>
75 </property>
76 <property name="text">
77 <string/>
78 </property>
79 </widget>
80 </item>
81 <item row="0" column="2">
82 <widget class="QLabel" name="m_labFileType">
83 <property name="frameShape">
84 <enum>QFrame::WinPanel</enum>
85 </property>
86 <property name="frameShadow">
87 <enum>QFrame::Sunken</enum>
88 </property>
89 <property name="text">
90 <string/>
91 </property>
92 </widget>
93 </item>
94 <item row="0" column="3">
95 <widget class="QCheckBox" name="m_checkDir">
96 <property name="sizePolicy">
97 <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
98 <horstretch>0</horstretch>
99 <verstretch>0</verstretch>
100 </sizepolicy>
101 </property>
102 <property name="text">
103 <string>目录</string>
104 </property>
105 </widget>
106 </item>
107 <item row="1" column="0" colspan="4">
108 <widget class="QLabel" name="m_labFilePath">
109 <property name="frameShape">
110 <enum>QFrame::WinPanel</enum>
111 </property>
112 <property name="frameShadow">
113 <enum>QFrame::Sunken</enum>
114 </property>
115 <property name="text">
116 <string/>
117 </property>
118 </widget>
119 </item>
120 </layout>
121 </item>
122 </layout>
123 </widget>
124 <tabstops>
125 <tabstop>m_tree</tabstop>
126 <tabstop>m_table</tabstop>
127 <tabstop>m_list</tabstop>
128 <tabstop>m_checkDir</tabstop>
129 </tabstops>
130 <resources/>
131 <connections>
132 <connection>
133 <sender>m_tree</sender>
134 <signal>clicked(QModelIndex)</signal>
135 <receiver>m_list</receiver>
136 <slot>setRootIndex(QModelIndex)</slot>
137 <hints>
138 <hint type="sourcelabel">
139 <x>203</x>
140 <y>140</y>
141 </hint>
142 <hint type="destinationlabel">
143 <x>596</x>
144 <y>275</y>
145 </hint>
146 </hints>
147 </connection>
148 <connection>
149 <sender>m_tree</sender>
150 <signal>clicked(QModelIndex)</signal>
151 <receiver>m_table</receiver>
152 <slot>setRootIndex(QModelIndex)</slot>
153 <hints>
154 <hint type="sourcelabel">
155 <x>203</x>
156 <y>140</y>
157 </hint>
158 <hint type="destinationlabel">
159 <x>203</x>
160 <y>409</y>
161 </hint>
162 </hints>
163 </connection>
164 </connections>
165</ui>
C:\Users\Minwei\Projects\Qt\FileSystem\filesystemdialog.h:
xxxxxxxxxx
311
2
3
4
5
6
7QT_BEGIN_NAMESPACE
8namespace Ui { class FileSystemDialog; }
9QT_END_NAMESPACE
10
11class FileSystemDialog : public QDialog
12{
13 Q_OBJECT
14
15public:
16 FileSystemDialog(QWidget *parent = nullptr);
17 ~FileSystemDialog();
18
19private slots:
20 void on_m_tree_clicked(const QModelIndex &index);
21 void on_m_table_clicked(const QModelIndex &index);
22 void on_m_list_clicked(const QModelIndex &index);
23
24private:
25 void updateLabels(QModelIndex const& index);
26
27 Ui::FileSystemDialog *ui;
28 QFileSystemModel* m_model;
29};
30
31// FILESYSTEMDIALOG_H
C:\Users\Minwei\Projects\Qt\FileSystem\filesystemdialog.cpp:
xxxxxxxxxx
501
2
3
4FileSystemDialog::FileSystemDialog(QWidget *parent)
5 : QDialog(parent)
6 , ui(new Ui::FileSystemDialog)
7 , m_model(new QFileSystemModel(this))
8{
9 ui->setupUi(this);
10
11 ui->m_splitterVer->setStretchFactor(0, 3);
12 ui->m_splitterVer->setStretchFactor(1, 2);
13
14 ui->m_splitterHor->setStretchFactor(0, 83);
15 ui->m_splitterHor->setStretchFactor(1, 50);
16
17 m_model->setRootPath(QDir::currentPath());
18 ui->m_tree->setModel(m_model);
19 ui->m_table->setModel(m_model);
20 ui->m_list->setModel(m_model);
21}
22
23FileSystemDialog::~FileSystemDialog()
24{
25 delete ui;
26}
27
28void FileSystemDialog::on_m_tree_clicked(const QModelIndex &index)
29{
30 updateLabels(index);
31}
32
33void FileSystemDialog::on_m_table_clicked(const QModelIndex &index)
34{
35 updateLabels(m_model->index(index.row(), 0, index.parent()));
36}
37
38void FileSystemDialog::on_m_list_clicked(const QModelIndex &index)
39{
40 updateLabels(index);
41}
42
43void FileSystemDialog::updateLabels(QModelIndex const& index)
44{
45 ui->m_labFileName->setText(m_model->fileName(index));
46 ui->m_labFileSize->setText(QString("%1 Bytes").arg(m_model->size(index)));
47 ui->m_labFileType->setText(m_model->type(index));
48 ui->m_checkDir->setChecked(m_model->isDir(index));
49 ui->m_labFilePath->setText(m_model->filePath(index));
50}
运行效果如图所示: