【Qt开发】QTableWidget的详细设置
在使用Qt不多的日子里,已經(jīng)兩次用到了QTableWidget這個控件,也慢慢的習(xí)慣和喜歡上了它。再使用QTableWidget的時候,已不像剛開始使用時的迷茫。嗯嗯。現(xiàn)在就來總結(jié)總結(jié)我與QTableWidget相識的歷程......(*^__^*)嘻嘻……
使用時也查過不少資料,在此感謝前輩們的用心總結(jié)與分享!
1.QTableWidget不能在mainwindow中隨主窗口的大小變化?
解決:在表格外部添加布局。
代碼:tableWidget=newQTableWidget;
tableWidget->setObjectName(QString::fromUtf8("tableWidget"));
QVBoxLayout*verticalLayout;
verticalLayout->addWidget(tableWidget);
2.將表格變?yōu)榻咕庉嫞?/p>
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
(參數(shù)含義:QAbstractItemView.NoEditTriggers--不能對表格內(nèi)容進行修改
QAbstractItemView.CurrentChanged--任何時候都能對單元格修改
QAbstractItemView.DoubleClicked--雙擊單元格
QAbstractItemView.SelectedClicked--單擊已選中的內(nèi)容
QAbstractItemView.EditKeyPressed--
QAbstractItemView.AnyKeyPressed--按下任意鍵就能修改
QAbstractItemView.AllEditTriggers--以上條件全包括)
3.設(shè)置表格為整行選擇
tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);//整行選中的方式
(參數(shù)含義:AbstractItemView.SelectItems--選中單個單元格
QAbstractItemView.SelectRows--選中一行
QAbstractItemView.SelectColumns--選中一列)
4.單個選中和多個選中的設(shè)置:
tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);//設(shè)置為可以選中多個目標(biāo)
(參數(shù)含義:QAbstractItemView.NoSelection--不能選擇
QAbstractItemView.SingleSelection--選中單個目標(biāo)
QAbstractItemView.MultiSelection--選中多個目標(biāo)
QAbstractItemView.ExtendedSelection/QAbstractItemView.ContiguousSelection的區(qū)別不明顯,主要功能是正常情況下是單選,但按下Ctrl或Shift鍵后,可以多選)
5.表格表頭的顯示與隱藏
對于水平或垂直方法的表頭,可以用以下方式進行隱藏/顯示的設(shè)置:
tableWidget->verticalHeader()->setVisible(false);//隱藏列表頭
tableWidget->horizontalHeader()->setVisible(false);//隱藏行表頭
注意:需要#include<QHeaderView>
6.對表頭文字的字體、顏色進行設(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>
7.在單元格里加入控件:
QComboBox*comBox=newQComboBox();
comBox->addItem("Y");
comBox->addItem("N");
tableWidget->setCellWidget(0,2,comBox);
8.單元格中添加圖片:
tableWidget->setItem(row,0,newQTableWidgetItem(QIcon(":/new/images/kingdemo.ico"),tr("")));
9設(shè)置單元格字體顏色、背景顏色和字體字符:
QTableWidgetItem*item=newQTableWidgetItem("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"));
10.設(shè)置單元格內(nèi)文字的對齊方式
水平對齊方式有:
ConstantValueDescription
Qt.AlignLeft0x0001Alignswiththeleftedge.
Qt.AlignRight0x0002Alignswiththerightedge.
Qt.AlignHCenter0x0004Centershorizontallyintheavailablespace.
Qt.AlignJustify0x0008Justifiesthetextintheavailablespace.
垂直對齊方式:
ConstantValueDescription
Qt.AlignTop0x0020Alignswiththetop.
Qt.AlignBottom0x0040Alignswiththebottom.
Qt.AlignVCenter0x0080Centersverticallyintheavailablespace.
如果兩種都要設(shè)置,只要用Qt.AlignHCenter|Qt.AlignVCenter的方式即可
11.合并單元格:
tableWidget->setSpan(0,0,3,1)#其參數(shù)為:要改變單元格的1行數(shù)、2列數(shù),要合并的3行數(shù)、4列數(shù)
12.設(shè)置單元格的大小
首先,可以指定某個行或者列的大小
tableWidget->setColumnWidth(3,200);
tableWidget->setRowHeight(3,60);
還可以將行和列的大小設(shè)為與內(nèi)容相匹配
tableWidget->resizeColumnsToContents();
tableWidget->resizeRowsToContents();
13.獲得單擊單元格的內(nèi)容
通過實現(xiàn)itemClicked(QTableWidgetItem*)信號的槽函數(shù),就可以獲得鼠標(biāo)單擊到的單元格指針,進而獲得其中的文字信息
connect(tableWidget,SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),this,SLOT(getItem(QTreeWidgetItem*,int)));
//將itemClicked信號與函數(shù)getItem綁定
14.QTableWidget要調(diào)整表格行寬主要涉及以下函數(shù)
tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);//使列完全填充并平分
tableWidget->verticalHeader()->setResizeMode(QHeaderView::Stretch);//行自適應(yīng)寬度
tableWidget->resizeColumnsToContents();//根據(jù)內(nèi)容調(diào)整列寬
tableWidget->resizeColumnToContents(intcol);//根據(jù)內(nèi)容自動調(diào)整給定列寬
tableWidget->horizontalHeader()->setResizeMode//把給定列設(shè)置為給定模式
主要模式有Stretch和Fixed
15.添加表頭內(nèi)容:
方法一:
QStringListheader;
header<<""<<tr("1")<<tr("2")<<tr("3")<<tr("4)<<tr("5");
方法二:
tableWidget->setHorizontalHeaderLabels(QStringList()<<tr("1")<<tr("2")<<tr("3")<<tr("4)<<tr("5"));
16.清除:
tableWidget->clear();//清除所有可見數(shù)據(jù)(包括表頭),行還在
tableWidget->clearContents();//只清除表中數(shù)據(jù),不清除表頭內(nèi)容
tableWidget->setRowCount(0);//連行也清除掉
15.一些零碎的知識點代碼:
introw=tableWidget->rowCount();//獲取表格中當(dāng)前總行數(shù)
tableWidget->setRowCount(row+1);//添加一行
tableWidget->removeRow(row);//清除已有的行列
Introw1=tableWidget->currentItem()->row();//當(dāng)前選中行
boolfocus=tableWidget->isItemSelected(tableWidget->currentItem());//判斷是否選中一行
QStringproName=tableWidget->item(row,col)->text();//獲取某一格內(nèi)容
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);//去掉自動滾動
17.排序:
tableWidget->sortByColumn(0,Qt::AscendingOrder);//顧名思義,該函數(shù)意思是將某列按升序/降序的方式排列
嗯嗯!暫時想到和用到的只有這么多了,再用再補。。。(參考了某些前輩的,不要介意哦,(*^__^*))
http://blog.csdn.net/mingxia_sui/article/details/7681863
總結(jié)
以上是生活随笔為你收集整理的【Qt开发】QTableWidget的详细设置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 抖音封禁了以后怎么办
- 下一篇: 笔趣阁小说免费阅读器