Qt中QTableWidget用法总结
QTableWidget是QT程序中常用的顯示數(shù)據(jù)表格的空間,很類似于VC、C#中的DataGrid。說到QTableWidget,就必須講一下它跟QTabelView的區(qū)別了。QTableWidget是QTableView的子類,主要的區(qū)別是QTableView可以使用自定義的數(shù)據(jù)模型來顯示內(nèi)容(也就是先要通過setModel來綁定數(shù)據(jù)源),而QTableWidget則只能使用標(biāo)準(zhǔn)的數(shù)據(jù)模型,并且其單元格數(shù)據(jù)是QTableWidgetItem的對象來實現(xiàn)的(也就是不需要數(shù)據(jù)源,將逐個單元格內(nèi)的信息填好即可)。這主要體現(xiàn)在QTableView類中有setModel成員函數(shù),而到了QTableWidget類中,該成員函數(shù)變成了私有。使用QTableWidget就離不開QTableWidgetItem。QTableWidgetItem用來表示表格中的一個單元格,正個表格都需要用逐個單元格構(gòu)建起來。
?
1 #include <QtGui/QApplication>2 #include <QTableWidget>
3 #include <QTableWidgetItem>
4
5 int main(int argc, char *argv[])
6 {
7 QApplication a(argc, argv);
8 QTableWidget *tableWidget = new QTableWidget(10,5); // 構(gòu)造了一個QTableWidget的對象,并且設(shè)置為10行,5列
9 // 也可用下面的方法構(gòu)造QTableWidget對象
10 // QTableWidget *tableWidget = new QTableWidget;
11 // tableWidget->setRowCount(10); //設(shè)置行數(shù)為10
12 // tableWidget->setColumnCount(5); //設(shè)置列數(shù)為5
13 tableWidget->setWindowTitle("QTableWidget & Item");
14 tableWidget->resize(350, 200); //設(shè)置表格
15 QStringList header;
16 header<<"Month"<<"Description";
17 tableWidget->setHorizontalHeaderLabels(header);
18 tableWidget->setItem(0,0,new QTableWidgetItem("Jan"));
19 tableWidget->setItem(1,0,new QTableWidgetItem("Feb"));
20 tableWidget->setItem(2,0,new QTableWidgetItem("Mar"));
21
22 tableWidget->setItem(0,1,new QTableWidgetItem(QIcon("images/IED.png"), "Jan's month"));
23 tableWidget->setItem(1,1,new QTableWidgetItem(QIcon("images/IED.png"), "Feb's month"));
24 tableWidget->setItem(2,1,new QTableWidgetItem(QIcon("images/IED.png"), "Mar's month"));
25 tableWidget->show();
26
27 return a.exec();
28 }
?
?
一. 對QTableWidget本身的效果實現(xiàn)
1. 將表格變?yōu)榻咕庉?/p>
在默認(rèn)情況下,表格里的字符是可以更改的,比如雙擊一個單元格,就可以修改原來的內(nèi)容,如果想禁止用戶的這種操作,讓這個表格對用戶只讀,可以這樣:
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
?
QAbstractItemView.NoEditTriggers是QAbstractItemView.EditTrigger枚舉中的一個,都是觸發(fā)修改單元格內(nèi)容的條件:
| QAbstractItemView.NoEditTriggers | 0 | No editing possible. 不能對表格內(nèi)容進行修改 |
| QAbstractItemView.CurrentChanged | 1 | Editing start whenever current item changes.任何時候都能對單元格修改 |
| QAbstractItemView.DoubleClicked | 2 | Editing starts when an item is double clicked.雙擊單元格 |
| QAbstractItemView.SelectedClicked | 4 | Editing starts when clicking on an already selected item.單擊已選中的內(nèi)容 |
| QAbstractItemView.EditKeyPressed | 8 | Editing starts when the platform edit key has been pressed over an item. |
| QAbstractItemView.AnyKeyPressed | 16 | Editing starts when any key is pressed over an item.按下任意鍵就能修改 |
| QAbstractItemView.AllEditTriggers | 31 | Editing starts for all above actions.以上條件全包括 |
?
?
2. 設(shè)置表格為整行選擇
tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);? //整行選中的方式
QAbstractItemView.SelectionBehavior枚舉還有如下類型
| QAbstractItemView.SelectItems | 0 | Selecting single items.選中單個單元格 |
| QAbstractItemView.SelectRows | 1 | Selecting only rows.選中一行 |
| QAbstractItemView.SelectColumns | 2 | Selecting only columns.選中一列 |
?
?
3.單個選中和多個選中的設(shè)置:
tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);? //設(shè)置為可以選中多個目標(biāo)
該函數(shù)的參數(shù)還可以是:
QAbstractItemView.NoSelection????? 不能選擇
QAbstractItemView.SingleSelection? 選中單個目標(biāo)
QAbstractItemView.MultiSelection??? 選中多個目標(biāo)
QAbstractItemView.ExtendedSelection?? QAbstractItemView.ContiguousSelection 的區(qū)別不明顯,主要功能是正常情況下是單選,但按下Ctrl或Shift鍵后,可以多選
?
4. 表格表頭的顯示與隱藏
對于水平或垂直方法的表頭,可以用以下方式進行 隱藏/顯示 的設(shè)置:
1 tableWidget->verticalHeader()->setVisible(false); //隱藏列表頭2 tableWidget->horizontalHeader()->setVisible(false); //隱藏行表頭
?
注意:需要 #include<QHeaderView>
?
5. 對表頭文字的字體、顏色進行設(shè)置
1 QTableWidgetItem *columnHeaderItem0 = tableWidget->horizontalHeaderItem(0); //獲得水平方向表頭的Item對象2 columnHeaderItem0->setFont(QFont("Helvetica")); //設(shè)置字體
3 columnHeaderItem0->setBackgroundColor(QColor(0,60,10)); //設(shè)置單元格背景顏色
4 columnHeaderItem0->setTextColor(QColor(200,111,30)); //設(shè)置文字顏色
QTableWidgetItem *columnHeaderItem0 = tableWidget->horizontalHeaderItem(0); //獲得水平方向表頭的Item對象 columnHeaderItem0->setFont(QFont("Helvetica")); //設(shè)置字體 columnHeaderItem0->setBackgroundColor(QColor(0,60,10)); //設(shè)置單元格背景顏色 columnHeaderItem0->setTextColor(QColor(200,111,30)); //設(shè)置文字顏色注意:需要 #include<QHeaderView>
?
6. 在單元格里加入控件:
QTableWidget不僅允許把文字加到單元格,還允許把控件也放到單元格中。比如,把一個下拉框加入單元格,可以這么做:
1 QComboBox *comBox = new QComboBox();2 comBox->addItem("Y");
3 comBox->addItem("N");
4 tableWidget->setCellWidget(0,2,comBox);
二. 對單元格的進行設(shè)置
1. 單元格設(shè)置字體顏色和背景顏色 及字體字符
1 QTableWidgetItem *item = new QTableWidgetItem("Apple");2 item->setBackgroundColor(QColor(0,60,10));
3 item->setTextColor(QColor(200,111,100));
4 item->setFont(QFont("Helvetica"));
5 tableWidget->setItem(0,3,item);
QTableWidgetItem *item = new QTableWidgetItem("Apple"); item->setBackgroundColor(QColor(0,60,10)); item->setTextColor(QColor(200,111,100)); item->setFont(QFont("Helvetica")); tableWidget->setItem(0,3,item);
另:如果需要對所有的單元格都使用這種字體,則可以使用? tableWidget->setFont(QFont("Helvetica"));
2. 設(shè)置單元格內(nèi)文字的對齊方式
這個比較簡單,使用newItem.setTextAlignment()函數(shù)即可,該函數(shù)的參數(shù)為單元格內(nèi)的對齊方式,和字符輸入順序是自左相右還是自右向左。
水平對齊方式有:
| Qt.AlignLeft | 0x0001 | Aligns with the left edge. |
| Qt.AlignRight | 0x0002 | Aligns with the right edge. |
| Qt.AlignHCenter | 0x0004 | Centers horizontally in the available space. |
| Qt.AlignJustify | 0x0008 | Justifies the text in the available space. |
?
垂直對齊方式:
| Qt.AlignTop | 0x0020 | Aligns with the top. |
| Qt.AlignBottom | 0x0040 | Aligns with the bottom. |
| Qt.AlignVCenter | 0x0080 | Centers vertically in the available space. |
?
如果兩種都要設(shè)置,只要用 Qt.AlignHCenter |? Qt.AlignVCenter 的方式即可
?
3. 合并單元格效果的實現(xiàn):
tableWidget->setSpan(0, 0, 3, 1)? # 其參數(shù)為: 要改變單元格的?? 1行數(shù)? 2列數(shù)???? 要合并的? 3行數(shù)? 4列數(shù)
?
4. 設(shè)置單元格的大小
首先,可以指定某個行或者列的大小
1 tableWidget->setColumnWidth(3,200);2 tableWidget->setRowHeight(3,60);
tableWidget->setColumnWidth(3,200); tableWidget->setRowHeight(3,60);
還可以將行和列的大小設(shè)為與內(nèi)容相匹配
1 tableWidget->resizeColumnsToContents();2 tableWidget->resizeRowsToContents();
tableWidget->resizeColumnsToContents(); tableWidget->resizeRowsToContents();
5. 獲得單擊單元格的內(nèi)容
通過實現(xiàn) itemClicked (QTableWidgetItem *) 信號的槽函數(shù),就可以獲得鼠標(biāo)單擊到的單元格指針,進而獲得其中的文字信息
connect(tableWidget,SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),this,SLOT(getItem(QTreeWidgetItem*,int)));
//將itemClicked信號與函數(shù)getItem綁定
6.QTableWidget要調(diào)整表格行寬主要涉及以下一個函數(shù)
resizeColumnsToContents();????????????????????? 根據(jù)內(nèi)容調(diào)整列寬????????? resizeColumnToContents(int col);?????????????? 根據(jù)內(nèi)容自動調(diào)整給定列寬 horizontalHeader()->setResizeMode?????????? 把給定列設(shè)置為給定模式 主要模式有Stretch和Fixed
7.
int row = rowCount(); removeRow(row);//清除已有的行列 setShowGrid(true);//顯示表格線 verticalHeader()->setVisible(false);//隱藏左邊垂直 QHeaderView *headerView = horizontalHeader(); headerView->setMovable(false);//去除表頭的移動 headerView->resizeSection(0,284);//設(shè)置第一列寬 headerView->resizeSection(1,127);//設(shè)置第二列寬 headerView->setResizeMode(QHeaderView::Fixed);//列表不能移動 headerView->setClickable(false);//不響應(yīng)鼠標(biāo)單擊 setEditTriggers(QTableWidget::NoEditTriggers);//不能編輯 setSelectionBehavior(QTableWidget::SelectRows);//一次選中一行 setSelectionMode(QAbstractItemView::SingleSelection);//只能單選 /*QScrollBar *scrollBar = horizontalScrollBar(); scrollBar->hide();*/ setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//去掉水平滾動條 setVerticalScrollMode(QAbstractItemView::ScrollPerItem);//垂直滾動條按項移動 setAutoScroll(false);//去掉自動滾動
轉(zhuǎn)載于:https://www.cnblogs.com/elect-fans/archive/2012/03/21/2408566.html
總結(jié)
以上是生活随笔為你收集整理的Qt中QTableWidget用法总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ASP.NET Web API 处理架构
- 下一篇: C++:cin.getline