QTableWidget 基本概念及功能使用

QTableWidget是QT程序中經常使用的顯示數據表格的空間。QTableWidget是QTableView的子類,主要的區別是QTableView可使用自定義的數據模型來顯示內容(也就是先要經過setModel來綁定數據源),而QTableWidget則只能使用標準的數據模型,而且其單元格數據是QTableWidgetItem的對象來實現的(也就是不須要數據源,將逐個單元格內的信息填好便可)。這主要體如今QTableView類中有setModel成員函數,而到了QTableWidget類中,該成員函數變成了私有。使用QTableWidget就離不開QTableWidgetItem。QTableWidgetItem用來表示表格中的一個單元格,整個表格都須要用逐個單元格構建起來。 html

view plain
#include <QtGui/QApplication> 
#include <QTableWidget> 
#include <QTableWidgetItem> 

int main(int argc, char *argv[]) 

QApplication a(argc, argv); 
QTableWidget *tableWidget = new QTableWidget(10,5); // 構造了一個QTableWidget的對象,而且設置爲10行,5列 
// 也可用下面的方法構造QTableWidget對象 
// QTableWidget *tableWidget = new QTableWidget; 
// tableWidget->setRowCount(10); //設置行數爲10 
// tableWidget->setColumnCount(5); //設置列數爲5 
tableWidget->setWindowTitle("QTableWidget & Item"); 
tableWidget->resize(350, 200); //設置表格 
QStringList header; 
header<<"Month"<<"Description"; 
tableWidget->setHorizontalHeaderLabels(header); 
tableWidget->setItem(0,0,new QTableWidgetItem("Jan")); 
tableWidget->setItem(1,0,new QTableWidgetItem("Feb")); 
tableWidget->setItem(2,0,new QTableWidgetItem("Mar")); 

tableWidget->setItem(0,1,new QTableWidgetItem(QIcon("images/IED.png"), "Jan's month")); 
tableWidget->setItem(1,1,new QTableWidgetItem(QIcon("images/IED.png"), "Feb's month")); 
tableWidget->setItem(2,1,new QTableWidgetItem(QIcon("images/IED.png"), "Mar's month")); 
tableWidget->show(); 

return a.exec(); 


一. 對QTableWidget自己的效果實現 
1. 將表格變爲禁止編輯

在默認狀況下,表格裏的字符是能夠更改的,好比雙擊一個單元格,就能夠修改原來的內容,若是想禁止用戶的這種操做,讓這個表格對用戶只讀,能夠這樣:
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

QAbstractItemView.NoEditTriggers是QAbstractItemView.EditTrigger枚舉中的一個,都是觸發修改單元格內容的條件:
QAbstractItemView.NoEditTriggers 

No editing possible. 不能對錶格內容進行修改

QAbstractItemView.CurrentChanged 
Editing start whenever current item changes.任什麼時候候都能對單元格修改
QAbstractItemView.DoubleClicked 
Editing starts when an item is double clicked.雙擊單元格
QAbstractItemView.SelectedClicked 
Editing starts when clicking on an already selected item.單擊已選中的內容
QAbstractItemView.EditKeyPressed 
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. 設置表格爲整行選擇 
tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //整行選中的方式
QAbstractItemView.SelectionBehavior枚舉還有以下類型
Constant 
Value 
Description

QAbstractItemView.SelectItems 

Selecting single items.選中單個單元格
QAbstractItemView.SelectRows 
Selecting only rows.選中一行
QAbstractItemView.SelectColumns 
Selecting only columns.選中一列

3.單個選中和多個選中的設置:
tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection); //設置爲能夠選中多個目標
該函數的參數還能夠是:
QAbstractItemView.NoSelection 不能選擇
QAbstractItemView.SingleSelection 選中單個目標
QAbstractItemView.MultiSelection 選中多個目標

QAbstractItemView.ExtendedSelection QAbstractItemView.ContiguousSelection 的區別不明顯,主要功能是正常狀況下是單選,但按下Ctrl或Shift鍵後,能夠多選

4. 表格表頭的顯示與隱藏
對於水平或垂直方法的表頭,能夠用如下方式進行 隱藏/顯示 的設置:
view plain
tableWidget->verticalHeader()->setVisible(false); //隱藏列表頭 
tableWidget->horizontalHeader()->setVisible(false); //隱藏行表頭 

注意:須要 #include <QHeaderView>

5. 對錶頭文字的字體、顏色進行設置 
view plain
QTableWidgetItem *columnHeaderItem0 = tableWidget->horizontalHeaderItem(0); //得到水平方向表頭的Item對象 
columnHeaderItem0->setFont(QFont("Helvetica")); //設置字體 
columnHeaderItem0->setBackgroundColor(QColor(0,60,10)); //設置單元格背景顏色 
columnHeaderItem0->setTextColor(QColor(200,111,30)); //設置文字顏色 

注意:須要 #include <QHeaderView>

6. 在單元格里加入控件:
QTableWidget不只容許把文字加到單元格,還容許把控件也放到單元格中。好比,把一個下拉框加入單元格,能夠這麼作:
view plain
QComboBox *comBox = new QComboBox(); 
comBox->addItem("Y"); 
comBox->addItem("N"); 
tableWidget->setCellWidget(0,2,comBox); 

二. 對單元格的進行設置 
1. 單元格設置字體顏色和背景顏色 及字體字符
view plain
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. 設置單元格內文字的對齊方式
這個比較簡單,使用newItem.setTextAlignment()函數便可,該函數的參數爲單元格內的對齊方式,和字符輸入順序是自左相右仍是自右向左。

水平對齊方式有:
Constant Value Description
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.

垂直對齊方式:
Constant Value Description
Qt::AlignTop 0x0020 Aligns with the top.
Qt::AlignBottom 0x0040 Aligns with the bottom.
Qt::AlignVCenter 0x0080 Centers vertically in the available space.

若是兩種都要設置,只要用 Qt::AlignHCenter | Qt:AlignVCenter 的方式便可 
居中對齊方式  Qt::AlignCenter

3. 合併單元格效果的實現:
tableWidget->setSpan(0, 0, 3, 1) # 其參數爲: 要改變單元格的 1行數 2列數 要合併的 3行數 4列數

4. 設置單元格的大小
首先,能夠指定某個行或者列的大小
view plain
tableWidget->setColumnWidth(3,200); 
tableWidget->setRowHeight(3,60); 

還能夠將行和列的大小設爲與內容相匹配
view plain
tableWidget->resizeColumnsToContents(); 
tableWidget->resizeRowsToContents(); 

5. 得到單擊單元格的內容 
經過實現 itemClicked (QTableWidgetItem *) 信號的槽函數,就能夠得到鼠標單擊到的單元格指針,進而得到其中的文字信息
connect(tableWidget,SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),this, SLOT( getItem(QTreeWidgetItem*,int)) );

//將itemClicked信號與函數getItem綁定 函數

另兩篇介紹的文章 http://edsionte.com/techblog/archives/3014  字體

http://www.cnblogs.com/rollenholt/archive/2012/04/10/2440322.html
ui

相關文章
相關標籤/搜索