当前位置:网站首页>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);}"
);
边栏推荐
- 万字长文带你了解多态的底层原理,这一篇就够了
- Unity3D - modification of the Inspector panel of the custom class
- 装饰器模式:Swift 实现
- Essential C# scripting skills for Unity developers
- idea 方法注释:自定义修改method的return和params,void不显示
- 谁能解答?从mysql的binlog读取数据到kafka,但是数据类型有Insert,updata,
- Kotlin Algorithm Getting Started with Rabbit Number Optimization and Expansion
- Kotlin算法入门计算质因数
- eureka和consul的区别
- nodejs微服务中跨域,请求,接口,参数拦截等功能
猜你喜欢
Alibaba Sentinel - Slot chain解析
万字长文带你了解多态的底层原理,这一篇就够了
企业服务器主机加固现状分析
持续集成/持续部署(2)Jenkins & SonarQube
Notable NFT development trends in 2022
C Primer Plus(6) 中文版 第1章 初识C语言 1.7 使用C语言的7个步骤
SDUT 2877: angry_birds_again_and_again
如何通过 IDEA 数据库管理工具连接 TDengine?
Nuget can't find the package problem
pycharm中绘图,显示不了figure窗口的问题
随机推荐
IPQ4019/IPQ4029 support WiFi6 MiniPCIe Module 2T2R 2×2.4GHz 2x5GHz MT7915 MT7975
[wxGlade learning] wxGlade environment configuration
Essential C# scripting skills for Unity developers
工业检测深度学习方法综述
Getting Started with Kotlin Algorithms Calculating Prime Numbers and Optimization
Features of LoRa Chips
gRPC系列(一) 什么是RPC?
js将table生成excel文件并去除表格中的多余tr(js去除表格中空的tr标签)
What should I do if the mysql data query causes the cup to be full because the query time span is too large
如何通过开源数据库管理工具 DBeaver 连接 TDengine
ASP.NET Core 6框架揭秘实例演示[32]:错误页面的集中呈现方式
欧拉函数(用欧拉筛法求欧拉函数)
UNITY gameobject代码中setacvtive(false)与面板中直接去掉勾 效果不一样
jenkins 流水线脚本详细解析Pipeline
无代码平台助力中山医院搭建“智慧化管理体系”,实现智慧医疗
Openlayers 聚合图、权重聚合图以及聚合图点击事件
盘点四个入门级SSL证书
企业服务器主机加固现状分析
Song of the Cactus - Massive Rapid Expansion (1)
中移链EOSJS实战使用