QTableWidget默認提供了水平表頭和垂直表頭,表頭的索引從1開始。
#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來更改了水平表頭。
