在这个案例中,借助QStandardItemModel模型和QTableView视图,以二维表格形式展示文本文件中的数据。第一步,先构建项目的基本框架和用户界面。
通过QtCreator,在C:\Users\Minwei\Projects\Qt路径下,创建名为Table的项目。
C:\Users\Minwei\Projects\Qt\Table\Table.qrc:
xxxxxxxxxx
151<RCC>
2 <qresource prefix="/">
3 <file>images/open.bmp</file>
4 <file>images/save.bmp</file>
5 <file>images/preview.bmp</file>
6 <file>images/append.bmp</file>
7 <file>images/insert.bmp</file>
8 <file>images/delete.bmp</file>
9 <file>images/left.bmp</file>
10 <file>images/center.bmp</file>
11 <file>images/right.bmp</file>
12 <file>images/bold.bmp</file>
13 <file>images/exit.bmp</file>
14 </qresource>
15</RCC>
C:\Users\Minwei\Projects\Qt\Table\tablewindow.ui:
xxxxxxxxxx
2301
2<ui version="4.0">
3 <class>TableWindow</class>
4 <widget class="QMainWindow" name="TableWindow">
5 <property name="geometry">
6 <rect>
7 <x>0</x>
8 <y>0</y>
9 <width>800</width>
10 <height>480</height>
11 </rect>
12 </property>
13 <property name="windowTitle">
14 <string>表格</string>
15 </property>
16 <widget class="QWidget" name="m_central">
17 <widget class="QSplitter" name="m_splitter">
18 <property name="geometry">
19 <rect>
20 <x>10</x>
21 <y>10</y>
22 <width>781</width>
23 <height>391</height>
24 </rect>
25 </property>
26 <property name="orientation">
27 <enum>Qt::Horizontal</enum>
28 </property>
29 <widget class="QTableView" name="m_table">
30 <property name="frameShape">
31 <enum>QFrame::WinPanel</enum>
32 </property>
33 </widget>
34 <widget class="QPlainTextEdit" name="m_edit">
35 <property name="frameShape">
36 <enum>QFrame::WinPanel</enum>
37 </property>
38 <property name="readOnly">
39 <bool>true</bool>
40 </property>
41 </widget>
42 </widget>
43 </widget>
44 <widget class="QStatusBar" name="m_statusBar"/>
45 <widget class="QToolBar" name="m_toolBar">
46 <property name="windowTitle">
47 <string>工具栏</string>
48 </property>
49 <property name="toolButtonStyle">
50 <enum>Qt::ToolButtonTextUnderIcon</enum>
51 </property>
52 <attribute name="toolBarArea">
53 <enum>TopToolBarArea</enum>
54 </attribute>
55 <attribute name="toolBarBreak">
56 <bool>false</bool>
57 </attribute>
58 <addaction name="m_actOpen"/>
59 <addaction name="m_actSave"/>
60 <addaction name="m_actPreview"/>
61 <addaction name="separator"/>
62 <addaction name="m_actAppend"/>
63 <addaction name="m_actInsert"/>
64 <addaction name="m_actDelete"/>
65 <addaction name="separator"/>
66 <addaction name="m_actLeft"/>
67 <addaction name="m_actCenter"/>
68 <addaction name="m_actRight"/>
69 <addaction name="m_actBold"/>
70 <addaction name="separator"/>
71 <addaction name="m_actExit"/>
72 </widget>
73 <action name="m_actOpen">
74 <property name="icon">
75 <iconset resource="Table.qrc">
76 <normaloff>:/images/open.bmp</normaloff>:/images/open.bmp</iconset>
77 </property>
78 <property name="text">
79 <string>打开</string>
80 </property>
81 <property name="toolTip">
82 <string>打开</string>
83 </property>
84 </action>
85 <action name="m_actSave">
86 <property name="icon">
87 <iconset resource="Table.qrc">
88 <normaloff>:/images/save.bmp</normaloff>:/images/save.bmp</iconset>
89 </property>
90 <property name="text">
91 <string>保存</string>
92 </property>
93 <property name="toolTip">
94 <string>保存</string>
95 </property>
96 </action>
97 <action name="m_actPreview">
98 <property name="icon">
99 <iconset resource="Table.qrc">
100 <normaloff>:/images/preview.bmp</normaloff>:/images/preview.bmp</iconset>
101 </property>
102 <property name="text">
103 <string>预览</string>
104 </property>
105 <property name="toolTip">
106 <string>预览</string>
107 </property>
108 </action>
109 <action name="m_actAppend">
110 <property name="icon">
111 <iconset resource="Table.qrc">
112 <normaloff>:/images/append.bmp</normaloff>:/images/append.bmp</iconset>
113 </property>
114 <property name="text">
115 <string>添加</string>
116 </property>
117 <property name="toolTip">
118 <string>添加</string>
119 </property>
120 </action>
121 <action name="m_actInsert">
122 <property name="icon">
123 <iconset resource="Table.qrc">
124 <normaloff>:/images/insert.bmp</normaloff>:/images/insert.bmp</iconset>
125 </property>
126 <property name="text">
127 <string>插入</string>
128 </property>
129 <property name="toolTip">
130 <string>插入</string>
131 </property>
132 </action>
133 <action name="m_actDelete">
134 <property name="icon">
135 <iconset resource="Table.qrc">
136 <normaloff>:/images/delete.bmp</normaloff>:/images/delete.bmp</iconset>
137 </property>
138 <property name="text">
139 <string>删除</string>
140 </property>
141 <property name="toolTip">
142 <string>删除</string>
143 </property>
144 </action>
145 <action name="m_actLeft">
146 <property name="icon">
147 <iconset resource="Table.qrc">
148 <normaloff>:/images/left.bmp</normaloff>:/images/left.bmp</iconset>
149 </property>
150 <property name="text">
151 <string>靠左</string>
152 </property>
153 <property name="toolTip">
154 <string>靠左</string>
155 </property>
156 </action>
157 <action name="m_actCenter">
158 <property name="icon">
159 <iconset resource="Table.qrc">
160 <normaloff>:/images/center.bmp</normaloff>:/images/center.bmp</iconset>
161 </property>
162 <property name="text">
163 <string>居中</string>
164 </property>
165 <property name="toolTip">
166 <string>居中</string>
167 </property>
168 </action>
169 <action name="m_actRight">
170 <property name="icon">
171 <iconset resource="Table.qrc">
172 <normaloff>:/images/right.bmp</normaloff>:/images/right.bmp</iconset>
173 </property>
174 <property name="text">
175 <string>靠右</string>
176 </property>
177 <property name="toolTip">
178 <string>靠右</string>
179 </property>
180 </action>
181 <action name="m_actBold">
182 <property name="checkable">
183 <bool>true</bool>
184 </property>
185 <property name="icon">
186 <iconset resource="Table.qrc">
187 <normaloff>:/images/bold.bmp</normaloff>:/images/bold.bmp</iconset>
188 </property>
189 <property name="text">
190 <string>加粗</string>
191 </property>
192 <property name="toolTip">
193 <string>加粗</string>
194 </property>
195 </action>
196 <action name="m_actExit">
197 <property name="icon">
198 <iconset resource="Table.qrc">
199 <normaloff>:/images/exit.bmp</normaloff>:/images/exit.bmp</iconset>
200 </property>
201 <property name="text">
202 <string>退出</string>
203 </property>
204 <property name="toolTip">
205 <string>退出</string>
206 </property>
207 </action>
208 </widget>
209 <resources>
210 <include location="Table.qrc"/>
211 </resources>
212 <connections>
213 <connection>
214 <sender>m_actExit</sender>
215 <signal>triggered()</signal>
216 <receiver>TableWindow</receiver>
217 <slot>close()</slot>
218 <hints>
219 <hint type="sourcelabel">
220 <x>-1</x>
221 <y>-1</y>
222 </hint>
223 <hint type="destinationlabel">
224 <x>399</x>
225 <y>299</y>
226 </hint>
227 </hints>
228 </connection>
229 </connections>
230</ui>
C:\Users\Minwei\Projects\Qt\Table\tablewindow.h:
xxxxxxxxxx
401
2
3
4
5
6
7QT_BEGIN_NAMESPACE
8namespace Ui { class TableWindow; }
9QT_END_NAMESPACE
10
11class TableWindow : public QMainWindow
12{
13 Q_OBJECT
14
15public:
16 TableWindow(QWidget *parent = nullptr);
17 ~TableWindow();
18
19private slots:
20 void on_m_actOpen_triggered();
21 void on_m_actSave_triggered();
22 void on_m_actPreview_triggered();
23
24 void on_m_actAppend_triggered();
25 void on_m_actInsert_triggered();
26 void on_m_actDelete_triggered();
27
28 void on_m_actLeft_triggered();
29 void on_m_actCenter_triggered();
30 void on_m_actRight_triggered();
31 void on_m_actBold_triggered(bool checked);
32
33private:
34 Ui::TableWindow *ui;
35 QLabel* m_labCurFile;
36 QLabel* m_labCellPos;
37 QLabel* m_labCellText;
38};
39
40// TABLEWINDOW_H
C:\Users\Minwei\Projects\Qt\Table\tablewindow.cpp:
xxxxxxxxxx
681
2
3
4TableWindow::TableWindow(QWidget *parent)
5 : QMainWindow(parent)
6 , ui(new Ui::TableWindow)
7 , m_labCurFile(new QLabel("当前文件:"))
8 , m_labCellPos(new QLabel(" 单元格位置:"))
9 , m_labCellText(new QLabel(" 单元格内容:"))
10{
11 ui->setupUi(this);
12
13 ui->m_splitter->setStretchFactor(0, 348);
14 ui->m_splitter->setStretchFactor(1, 152);
15 setCentralWidget(ui->m_splitter);
16
17 m_labCurFile->setMinimumWidth(420);
18 ui->m_statusBar->addWidget(m_labCurFile);
19 m_labCellPos->setMinimumWidth(190);
20 ui->m_statusBar->addWidget(m_labCellPos);
21 m_labCellText->setMinimumWidth(190);
22 ui->m_statusBar->addWidget(m_labCellText);
23}
24
25TableWindow::~TableWindow()
26{
27 delete ui;
28}
29
30void TableWindow::on_m_actOpen_triggered()
31{
32}
33
34void TableWindow::on_m_actSave_triggered()
35{
36}
37
38void TableWindow::on_m_actPreview_triggered()
39{
40}
41
42void TableWindow::on_m_actAppend_triggered()
43{
44}
45
46void TableWindow::on_m_actInsert_triggered()
47{
48}
49
50void TableWindow::on_m_actDelete_triggered()
51{
52}
53
54void TableWindow::on_m_actLeft_triggered()
55{
56}
57
58void TableWindow::on_m_actCenter_triggered()
59{
60}
61
62void TableWindow::on_m_actRight_triggered()
63{
64}
65
66void TableWindow::on_m_actBold_triggered(bool checked)
67{
68}
运行效果如图所示: