詳解 QT Tablewidget 那些事
詳解 QT Tablewidget 那些事是本文介紹的內(nèi)容,從網(wǎng)上查了相關(guān)的資料,挺多的,于是推薦這篇文章與友們分享,希望在你學(xué)習(xí)的過程中對你有幫助,不多說,先來看內(nèi)容。QTableWidget可以顯示一個表格元件,表格中每個儲存格則為一個QTableWidgetItem的實例,QTableWidgetItem要安插至表格中哪個儲存格,則是依索引的指定來決定。
下面的程式碼為簡單的QTableWidget與QTableWidgetItem的示範(fàn):
- #include <QApplication>
- #include <QTableWidget>
- #include <QHBoxLayout>
- int main(int argc, char *argv[]) {
- QApplication app(argc, argv);
- QTableWidget *tableWidget = new QTableWidget;
- tableWidget->setWindowTitle("QTableWidget & Item");
- tableWidget->resize(350, 200);
- tableWidget->setRowCount(10);
- tableWidget->setColumnCount(5);
- QStringList header;
- header.append("Month");
- header.append("Description");
- tableWidget->setHorizontalHeaderLabels(header);
- tableWidget->setItem(0, 0, new QTableWidgetItem("January"));
- tableWidget->setItem(1, 0, new QTableWidgetItem("February"));
- tableWidget->setItem(2, 0, new QTableWidgetItem("March"));
- tableWidget->setItem(0, 1,
- new QTableWidgetItem(QIcon("caterpillar_head.jpg"), "caterpillar's month"));
- tableWidget->setItem(1, 1, new QTableWidgetItem(QIcon("momor_head.jpg"), "momor's month"));
- tableWidget->setItem(2, 1, new QTableWidgetItem(QIcon("bush_head.jpg"), "bush's month"));
- tableWidget->show();
- return app.exec();
- }
使用setItem()時必須指定儲存格索引值,索引為列(row)行(column),皆從0開始,最左上角即為索引(0, 0)位置。QTableWidgetItem也可以設(shè)置圖片或核取狀態(tài)(setCheckState())等。
下圖為程式執(zhí)行時的畫面:
小結(jié):關(guān)于詳解 QT Tablewidget 那些事的內(nèi)容到這里介紹完了,希望本文對你有所幫助!