当前位置:网站首页>QTableWidget 使用方法
QTableWidget 使用方法
2022-08-11 08:56:00 【蜉蝣之翼*】
行列
添加表头,隐藏/显示 行号
//设置列表头
QStringList labels;
labels <<" "<< QStringLiteral("编号") << QStringLiteral("速度") << QStringLiteral("操作") << " ";
ui.tableWidget->setColumnCount(5);
ui.tableWidget->setHorizontalHeaderLabels(labels);
//显示行号列
QHeaderView* headerView = ui->tableWidget->verticalHeader();
headerView->setHidden(false); //false 显示行号列 true Hide
关于QTableWidget 表头设置无效的原因
表头无效一般是由于 未设置列数量;
调用 setColumnCount 后,再调用 setHorizontalHeaderLabels 即可生效;;
当然最好的情况是 setHorizontalHeaderLabels 时根据参数的大小自动设置列数; 但QT未支持;
调整表头大小对齐
tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);//使列完全填充并平分
tableWidget->verticalHeader()->setResizeMode(QHeaderView::Stretch);//行自适应宽度
tableWidget->resizeColumnsToContents(); //根据内容调整列宽
tableWidget->resizeColumnToContents(int col);//根据内容自动调整给定列宽
tableWidget->horizontalHeader()->setResizeMode//把给定列设置为给定模式
//主要模式有Stretch和Fixed
QTableWidget列宽随心设置
tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); //x先自适应宽度
tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents); //然后设置要根据内容使用宽度的列
表头样式参考
去掉表头分割竖线
ui.tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{border:none;background-color:rgb(255,255,255);color: black;height: 32px;}");
添加数据
ui.tableWidget->setRowCount(rowNum);
for ( 。。。)
{
QTableWidgetItem* pRowItem =new QTableWidgetItem(row);
pRowItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);//单元格内容居中对齐
ui.tableWidget->setItem(row, 1, pRowItem);
QString strPositionName = item.vecColumnInfo.at(0).strText;
QTableWidgetItem* pName = new QTableWidgetItem(strPositionName);
pName->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
ui.tableWidget->setItem(row, 2, pName);
}
需要注意:
使用setItem插入数据之前要设置行数:ui.tableWidget->setRowCount(rowNum);
也可以在for里面动态设置行数
for( )
{
...
ui.tableWidget->setRowCount(row+1);
ui.tableWidget->setItem(row, ...);
}
插入的数据不显示
1、如果是调用insertRow插入行的,再调setRowHeight设置行高就可以了。
2、如果不是,那可能是rowCount返回的是0,这需要调setRowCount设置行数或者调insertRow插入行,再调setRowHeight设置行高就可以了
QTableWidget中添加QPushButton
// 创建QPushButton控件
QPushButton *pBtn = new QPushButton();
// 绑定信号
connect(pBtn, SIGNAL(clicked()), this, SLOT(OnBtnClicked()));
// 在QTableWidget中添加控件
tableWidget->setCellWidget(0,0,pBtn);
// 绑定的响应函数
void OnBtnClicked(void)
{
QPushButton *senderObj=qobject_cast<QPushButton*>(sender());
if(senderObj == nullptr)
{
return;
}
QModelIndex idx =tableWidget->indexAt(QPoint(senderObj->frameGeometry().x(),senderObj->frameGeometry().y()));
int row=idx.row();
// 其他响应信息......
}
样式
去掉网格线
ui.tableWidget->setShowGrid(false);
去掉 QTabWidget 的边框
ui.tableWidget->setStyleSheet("QTableWidget{border: none;}")
例子
去掉表头分割线,去掉表的边框和网格线,只留下横线
ui.tableWidget->setShowGrid(false);
ui.tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{border:none;background-color:rgb(255,255,255);color: black;height: 32px;}");
ui.tableWidget->setStyleSheet(
"QTableWidget{border: none;}"
"QTableWidget::Item{border:0px solid rgb(255,255,255);border-bottom:1px solid rgb(232,232,232);background-color:rgb(255,255,255);color: rgb(89,89,89);}"
"QTableWidget::Item:selected{background-color:rgb(255,255,255);color: rgb(24,144,255);}"
);
边栏推荐
- Linux,Redis中IOException: 远程主机强迫关闭了一个现有的连接。解决方法
- Getting Started with Kotlin Algorithm to Calculate the Number of Daffodils
- Initial use of IDEA
- 设置Vagrant创建的虚拟机名称和内存
- Kotlin算法入门计算水仙花数
- nodejs微服务中跨域,请求,接口,参数拦截等功能
- WordpressCMS主题开发02-制作顶部header.php和footer.php
- Kotlin Algorithm Getting Started with Rabbit Number Optimization and Expansion
- Song of the Cactus - Massive Rapid Expansion (1)
- 企业服务器主机加固现状分析
猜你喜欢

LoRa芯片的特征

新一代开源免费的轻量级 SSH 终端,非常炫酷好用!

Notable NFT development trends in 2022

Unity3D——自定义类的Inspector面板的修改

Continuous Integration/Continuous Deployment (2) Jenkins & SonarQube

Features of LoRa Chips

深度学习100例 —— 卷积神经网络(CNN)识别验证码

IPQ4019/IPQ4029 support WiFi6 MiniPCIe Module 2T2R 2×2.4GHz 2x5GHz MT7915 MT7975

Inventorying Four Entry-Level SSL Certificates

笔试题大疆08.07
随机推荐
兼容并蓄广纳百川,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang复合容器类型的声明和使用EP04
Openlayers 聚合图、权重聚合图以及聚合图点击事件
Filesystem Hierarchy Standard
关于ts的一些泛型关键字用法
轻量级网络(一):MobileNet V1,V2, V3系列
shell之sed
小目标检测3_注意力机制_Self-Attention
tensorflow 基础操作1(tensor 基本属性 , 维度变换,数学运算)
关于ts中的指针问题call,bind, apply
excel将数据按某一列值分组并绘制分组折线图
基于consul的注册发现的微服务架构迁移到servicemesh
wordpress插件开发02-首页文章自动摘要插件开发
MySql的索引
idea 方法注释:自定义修改method的return和params,void不显示
Typescript基本类型---上篇
如何在移动钱包中搭建一个小程序应用商店
IDEA的初步使用
2022-08-09 顾宇佳 学习笔记
1.3版本自定义TrainOneStepCell报错
万字长文带你了解多态的底层原理,这一篇就够了