QTableWidget实用技巧(转)
http://blog.csdn.NET/mingxia_sui/article/details/7681863
在使用Qt不多的日子里,已经两次用到了QTableWidget这个控件,也慢慢的习惯和喜欢上了它。再使用QTableWidget的时候,已不像刚开始使用时的迷茫。嗯嗯。现在就来总结总结我与QTableWidget相识的历程......(*^__^*) 嘻嘻……
使用时也查过不少资料,在此感谢前辈们的用心总结与分享!
1.QTableWidget不能在mainwindow中随主窗口的大小变化?
解决:在表格外部添加布局。
代码:tableWidget = new QTableWidget;
tableWidget ->setObjectName(QString::fromUtf8("tableWidget"));
QVBoxLayout *verticalLayout;
verticalLayout->addWidget(tableWidget );
2.将表格变为禁止编辑:
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
(参数含义:QAbstractItemView.NoEditTriggers--不能对表格内容进行修改
QAbstractItemView.CurrentChanged--任何时候都能对单元格修改
QAbstractItemView.DoubleClicked--双击单元格
QAbstractItemView.SelectedClicked--单击已选中的内容
QAbstractItemView.EditKeyPressed--
QAbstractItemView.AnyKeyPressed--按下任意键就能修改
QAbstractItemView.AllEditTriggers--以上条件全包括)
3.设置表格为整行选择
tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
(参数含义:AbstractItemView.SelectItems--选中单个单元格
QAbstractItemView.SelectRows--选中一行
QAbstractItemView.SelectColumns--选中一列)
4.单个选中和多个选中的设置:
tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection); //设置为可以选中多个目标
(参数含义:QAbstractItemView.NoSelection--不能选择
QAbstractItemView.SingleSelection--选中单个目标
QAbstractItemView.MultiSelection--选中多个目标
QAbstractItemView.ExtendedSelection/QAbstractItemView.ContiguousSelection 的区别不明显,主要功能是正常情况下是单选,但按下Ctrl或Shift键后,可以多选)
5.表格表头的显示与隐藏
对于水平或垂直方法的表头,可以用以下方式进行 隐藏/显示 的设置:
tableWidget->verticalHeader()->setVisible(false); //隐藏列表头
tableWidget->horizontalHeader()->setVisible(false); //隐藏行表头
注意:需要 #include <QHeaderView>
6.对表头文字的字体、颜色进行设置
QTableWidgetItem *columnHeaderItem0 = tableWidget->horizontalHeaderItem(0); //获得水平方向表头的Item对象
columnHeaderItem0->setFont(QFont("Helvetica")); //设置字体
columnHeaderItem0->setBackgroundColor(QColor(0,60,10)); //设置单元格背景颜色
columnHeaderItem0->setTextColor(QColor(200,111,30)); //设置文字颜色
注意:需要 #include <QHeaderView>
7.在单元格里加入控件:
QComboBox *comBox = new QComboBox();
comBox->addItem("Y");
comBox->addItem("N");
tableWidget->setCellWidget(0,2,comBox);
8.单元格中添加图片:
tableWidget->setItem(row, 0, new QTableWidgetItem(QIcon(":/new/images/kingdemo.ico"),tr("")));
9设置单元格字体颜色、背景颜色和字体字符:
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"));
10.设置单元格内文字的对齐方式
水平对齐方式有:
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 的方式即可
11.合并单元格:
tableWidget->setSpan(0, 0, 3, 1) # 其参数为: 要改变单元格的1行数、2列数,要合并的3行数、4列数
12.设置单元格的大小
首先,可以指定某个行或者列的大小
tableWidget->setColumnWidth(3,200);
tableWidget->setRowHeight(3,60);
还可以将行和列的大小设为与内容相匹配
tableWidget->resizeColumnsToContents();
tableWidget->resizeRowsToContents();
13.获得单击单元格的内容
通过实现 itemClicked (QTableWidgetItem *) 信号的槽函数,就可以获得鼠标单击到的单元格指针,进而获得其中的文字信息
connect(tableWidget,SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),this,SLOT(getItem(QTreeWidgetItem*,int)));
//将itemClicked信号与函数getItem绑定
14.QTableWidget要调整表格行宽主要涉及以下函数
tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);//使列完全填充并平分
tableWidget->verticalHeader()->setResizeMode(QHeaderView::Stretch);//行自适应宽度
tableWidget->resizeColumnsToContents(); //根据内容调整列宽
tableWidget->resizeColumnToContents(int col);//根据内容自动调整给定列宽
tableWidget->horizontalHeader()->setResizeMode//把给定列设置为给定模式
主要模式有Stretch和Fixed
15.添加表头内容:
方法一:
QStringList header;
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();//清除所有可见数据(包括表头),行还在
tableWidget->clearContents();//只清除表中数据,不清除表头内容
tableWidget->setRowCount(0);//连行也清除掉
15.一些零碎的知识点代码:
int row = tableWidget->rowCount();//获取表格中当前总行数
tableWidget->setRowCount(row+1);//添加一行
tableWidget->removeRow(row);//清除已有的行列
Int row1 = tableWidget->currentItem()->row();//当前选中行
bool focus = tableWidget->isItemSelected(tableWidget->currentItem());//判断是否选中一行
QString proName = tableWidget->item(row, col)->text();//获取某一格内容
setShowGrid(true);//显示表格线
verticalHeader()->setVisible(false);//隐藏左边垂直
QHeaderView *headerView = horizontalHeader();
headerView->setMovable(false);//去除表头的移动
headerView->resizeSection(0,284);//设置第一列宽
headerView->resizeSection(1,127);//设置第二列宽
headerView->setResizeMode(QHeaderView::Fixed);//列表不能移动
headerView->setClickable(false);//不响应鼠标单击
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);//顾名思义,该函数意思是将某列按升序/降序的方式排列
嗯嗯!暂时想到和用到的只有这么多了,再用再补。。。(参考了某些前辈的,不要介意哦,(*^__^*) )
Qt5以上的版本,所以其中有一项“headerView->setClickable(false);//不响应鼠标单击”,这个setClickable(false);函数已经被setSectionsClickable(false);所代替
QTableWidgetItem *nickItem = new QTableWidgetItem(nick);
QTableWidgetItem *hostNameItem = new QTableWidgetItem(hostName);
QTableWidgetItem *hostIpItem = new QTableWidgetItem(hostIp);
this->ui->friendList->insertRow(0);
this->ui->friendList->setItem(0, 0, nickItem);
this->ui->friendList->setItem(0, 1, hostNameItem);
this->ui->friendList->setItem(0, 2, hostIpItem);
this->ui->friendList->setSelectionBehavior(QAbstractItemView::SelectRows); //单击选择一行
this->ui->friendList->setSelectionMode(QAbstractItemView::SingleSelection); //设置只能选择一行,不能多行选中
this->ui->friendList->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置每行内容不可更改
this->ui->friendList->setAlternatingRowColors(true); //设置隔一行变一颜色,即:一灰一白
QTableWidget实用技巧(转)的更多相关文章
- Notepad++ 实用技巧
Notepad++是一款开源的文本编辑器,功能强大.很适合用于编辑.注释代码.它支持绝大部分主流的编程语言. 本文主要列举了本人在实际使用中遇到的一些技巧. 快捷键 自定义快捷键 首先,需要知道的是: ...
- javascript实用技巧、javascript高级技巧
字号+作者:H5之家 来源:H5之家 2016-10-31 11:00 我要评论( ) 三零网提供网络编程. JavaScript 的技术文章javascript实用技巧.javascript高级技巧 ...
- iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式
iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式 说明: 1)该文简短介绍在iOS开发中遍历字典.数组和集合的几种常见方式. 2)该文对应的代码可以在下面的地址获得:https:// ...
- iOS开发实用技巧—在手机浏览器头部弹出app应用下载提示
iOS开发实用技巧—在手机浏览器头部弹出app应用下载提示 本文介绍其简单使用: 第一步:在本地建立一个访问的服务端. 打开本地终端,在本地新建一个文件夹,在该文件夹中存放测试的html页面. ...
- iOS开发实用技巧—项目新特性页面的处理
iOS开发实用技巧篇—项目新特性页面的处理 说明:本文主要说明在项目开发中会涉及到的最最简单的新特性界面(实用UIScrollView展示多张图片的轮播)的处理. 代码示例: 新建一个专门的处理新特性 ...
- IOS 网络浅析-(十三 SDWebImage 实用技巧)
IOS 网络浅析-(十三 SDWebImage 实用技巧) 首先让我描述一下为了什么而产生的实用技巧.(在TableView.CollectionView中)当用户所处环境WiFi网速不够快(不能立即 ...
- NSString的八条实用技巧
NSString的八条实用技巧 有一篇文章写了:iOS开发之NSString的几条实用技巧 , 今天这篇,我们讲讲NSString的八条实用技巧.大家可以收藏起来,方便开发随时可以复制粘贴. 0.首字 ...
- ###《VIM实用技巧》
###<VIM实用技巧> #@author: gr #@date: 2015-11-20 #@email: forgerui@gmail.com <VIM实用技巧>阅读笔记. ...
- PowerDesigner实用技巧小结(3)
PowerDesigner实用技巧小结(3) PowerDesigner 技巧小结 sqlserver数据库databasevbscriptsqldomain 1.PowerDesigner 使用 M ...
随机推荐
- 【转载】jQuery1.5之后的deferred对象详解
原文:http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html 原文作者 ...
- Gradle version 2.2 is required. Current version is 2.14.1.
gradle版本错误: 1. 修改gradle\wrapper\gradle-wrapper.properties文件: distributionUrl=https\://services.gradl ...
- 【ros】.bag文件
Bags are typically created by a tool like rosbag They store the serialized message data in a file as ...
- python核心编程第六章练习6-12
6-12.字符串.(a)创建一个名字为findchr()的函数,函数声明如下.def findchr(string, char)findchr()要在字符串string中查找字符char,找到就返回该 ...
- ImportError: No module named MySQLdb
ImportError: No module named MySQLdb 该错误是源于我们没有安装Python连接MySQL所需的MySQLdb库而引起. python3.5下的解决方法ubuntu系 ...
- webstorm 注册码,亲测可用
WebStorm注册码 User Name: EMBRACE License Key: ===== LICENSE BEGIN ===== 24718-12042010 00001h6wzKLpfo3 ...
- hessian 协议
什么是Hessian协议呢? 目前,Web服务技术是解决异构平台系统的集成及互操作问题的主流技术. 它所基于的XML已经是Internet上交换数据的实际标准,基于通用的进程间通信协议和网络传输协议屏 ...
- 使用Git进行代码管理的心得
使用GitHub进行代码托管的方法 我是直接下载 Github for Windows ,这个软件自带一个 Github 客户端 和一个 Git shell,其中Github是图形化界面,对初学者来说 ...
- HTML 段落<p>标签
<p> 标签定义段落. p 元素会自动在其前后创建一些空白.浏览器会自动添加这些空间,您也可以在样式表中规定. 段间距IE默认为19px,通过p的margin-top属性设置FF默认为1. ...
- iPhone/iPad/Android UI尺寸规范
iPhone界面尺寸