QTableWidget表头中增加checkbox以及QTableWidget各种小知识
以及 知识 各种 增加 checkbox 表头 QTableWidget
2023-09-14 09:07:01 时间
QTableWidget能满足大部分需求,但有时我们需要给QTableWidget增加一些小功能:
部分效果图:
1、可以在表头中增加checkbox,方法如下:
①、可以引用头文件scheckboxheaderview.h
#ifndef SCHECKBOXHEADERVIEW_H
#define SCHECKBOXHEADERVIEW_H
#include <QtGui>
#include <QPainter>
#include <QHeaderView>
#include <QStyleOptionButton>
#include <QStyle>
class SCheckBoxHeaderView : public QHeaderView
{
Q_OBJECT
private:
bool isChecked;
int m_checkColIdx;
public:
SCheckBoxHeaderView( int checkColumnIndex, Qt::Orientation orientation, QWidget * parent = 0) :
QHeaderView(orientation, parent) {
m_checkColIdx = checkColumnIndex;
isChecked = false;
}
signals:
void checkStausChange(bool);
protected:
void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const {
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
painter->restore();
if (logicalIndex == m_checkColIdx) {
QStyleOptionButton option;
int width = 10;
for (int i=0; i<logicalIndex; ++i)
width += sectionSize( i );
option.rect = QRect(3, 5, 21, 21);
if (isChecked)
option.state = QStyle::State_On;
else
option.state = QStyle::State_Off;
this->style()->drawControl(QStyle::CE_CheckBox, &option, painter);
}
}
void mousePressEvent(QMouseEvent *event) {
if (visualIndexAt(event->pos().x()) == m_checkColIdx) {
isChecked = !isChecked;
this->updateSection(m_checkColIdx);
emit checkStausChange(isChecked);
}
QHeaderView::mousePressEvent(event);
}
};
#endif // SCHECKBOXHEADERVIEW_H
②、引用并使用
m_checkHeader = new SCheckBoxHeaderView(0, Qt::Horizontal, this);
this->setHorizontalHeader(m_checkHeader); // 这个this指针的父为QTableWidget
connect(m_checkHeader, &SCheckBoxHeaderView::checkStausChange, [=](bool check){
qDebug() << "is:" <<check;
});
2、在表格中设置某列(或某行)的最小(或最大)大小
QTableWidget提供了setColumnWidth()和setRowHeight()两个方法来设置某列(或某行)大小
QHeaderView提供了sectionResized信号,它的参数是这样的:void sectionResized(int logicalIndex, int oldSize, int newSize);
我们可以自建类(也可以直接用上述的1中我建好的类),然后继承(或组合)QHeaderView类,然后通过信号与槽来做处理,这里提供了一个方法。
这里以设置某列最小值为例:
bool m_min = false;
int m_minColumn = 0;
int m_minWidth = 20;
// m_checkHeader 为上述1中建好的类
connect(m_checkHeader, &SCheckBoxHeaderView::sectionResized, [=](int logicalIndex, int
oldSize, int newSize){
if(m_min) {
if(m_minColumn == logicalIndex) {
if(newSize < m_minWidth) {
this->setColumnWidth(m_minColumn, m_minWidth);
}
}
}
});
void setColumnMinWidth(int column, int width) {
this->setColumnWidth(column, width);
m_min = true;
m_minWidth = width;
m_minColumn = column;
}
3、显示表格线 (QTableWidget提供了此方法)
setShowGrid(true);
4、设置表格内容不可编辑(QTableWidget提供了此方法)
setEditTriggers(QAbstractItemView::NoEditTriggers);
5、设置表格内表头字体加粗或者怎样,这里以加粗为例
QFont font = this->horizontalHeader()->font();
font.setBold(true);
this->horizontalHeader()->setFont(font); // 这个this指针的父为QTableWidget
6、设置表头背景色
this->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}"); // 这个this指针的父为QTableWidget
7、设置表格列宽均等分
this->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//使列完全填充并平分
8、设置表格某单元块内容,主要是QTableWidget的setCellWidget方法,用法可以参考下面的:
STableWidgetItem* title = new STableWidgetItem(row, QSize(wid - 1, 29),this);
title->setText(str);
title->setDeleteVisible(delVisible);
this->setCellWidget(row, i, title);
这里STableWidgetItem类是我自己创建的QWidget,可以自己添加QWidget中的内容,比如复选框、按钮等
9、表格单元格内字体居中 QTableWidgetItem
QTableWidgetItem* item = new QTableWidgetItem(str);
item->setTextAlignment(Qt::AlignCenter);
this->setItem(row, i, item); // 这个this指针的父为QTableWidget
STableWidgetItem* title = new STableWidgetItem(row, QSize(wid - 1, 29),this);
相关文章
- 浅谈云上攻防系列——云IAM原理&风险以及最佳实践
- CUDA优化冷知识23|如何执行配置优化以及对性能调优的影响
- Python获取当前位置所在行数以及函数名
- 物联网概念以及发展趋势周边知识学习总结
- PsExec流量日志分析以及实现
- 知识兔Photoshop PS 2023软件安装包免费下载以及安装教程
- 当下做前端开发,不算简单,这篇文章可以让少走很多弯路以及需要掌握的知识
- vue封装组件以及调用「建议收藏」
- 【一】知识图谱基础概念、开发流程以及落地策略
- 电路仿真设计软件Multisim下载,Multisim 2023下载以及安装教程
- 上标下标字母和数字,以及如何使用
- 【2023新书】《ChatGPT在做什么…以及它为什么好使》
- 关于素数的一些知识以及算法详解编程语言
- 以及未来发展Linux分支及其未来发展趋势(linux的分支)
- 深入理解Oracle触发器类型以及应用(oracle触发器类型)
- 每周开源点评:Kubernetes 网络、OpenStack Train 以及更多的行业趋势
- js动态加载以及确定加载完成的代码
- mysql5.0入侵测试以及防范方法分享
- 理解web服务器和数据库的负载均衡以及反向代理