QTableWidget默认提供了水平表头和垂直表头,表头的索引从1开始。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
#include <QApplication>
#include <QTableWidget>
#include <QHBoxLayout>
int
main(
int
argc,
char
** argv)
{
QApplication app(argc, argv);
QTableWidget* tableWidget =
new
QTableWidget;
tableWidget->setWindowTitle(
"QTableWidget"
);
tableWidget->resize(350, 250);
tableWidget->setRowCount(6);
tableWidget->setColumnCount(3);
/*
QStringList header;
header.append("One");
header.append("Two");
tableWidget->setHorizontalHeaderLabels(header);
*/
tableWidget->setItem(0,0,
new
QTableWidgetItem(
"A"
));
tableWidget->setItem(1,0,
new
QTableWidgetItem(
"B"
));
tableWidget->setItem(2,0,
new
QTableWidgetItem(
"C"
));
tableWidget->setItem(3,0,
new
QTableWidgetItem(
"D"
));
tableWidget->setItem(4,0,
new
QTableWidgetItem(
"E"
));
tableWidget->setItem(0,1,
new
QTableWidgetItem(QIcon(
"images/data.png"
),
"data"
));
tableWidget->setItem(1,1,
new
QTableWidgetItem(QIcon(
"images/decision.png"
),
"decision"
));
tableWidget->setItem(2,1,
new
QTableWidgetItem(QIcon(
"images/document.png"
),
"document"
));
tableWidget->setItem(3,1,
new
QTableWidgetItem(QIcon(
"images/printer.png"
),
"printer"
));
tableWidget->setItem(4,1,
new
QTableWidgetItem(QIcon(
"images/process.png"
),
"process"
));
tableWidget->show();
return
app.exec();
}
|
运行如下图:
把注释去掉,运行如下图:
我们通过setHorizontalHeaderLabels来更改了水平表头。
==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/05/22/2513769.html,如需转载请自行联系原作者