QTableWidget表头添加复选框点击选中

0 1255
索鸟 2021-03-11
需要:0索币

在使用QTableWidget时需要在表头添加全选复选框,但是默认的表头无法添加复选框,只能用图片画上去一个复选框了。并且实现点击复选框时,发出信号,让QTableWidget中每条记录的复选框都选中,取消时,QTableWidget中每条记录的复选框都取消。并且实现复选框的normal、hov、pressed三种状态的显示。三种状态需要设置背景图片。效果图如下

                       

(1)   继承QHeaderView,重写绘制函数,绘制表头,重写鼠标移动函数,用来获取鼠标位置,鼠标移动到复选按钮上时,显示高亮。重写鼠标点击函数,点击复选框时,发送信号控制选中和取消。

头文件:

#pragma once

#include <QHeaderView>

#include <QPainter>

#include <QMouseEvent>

using namespace std;

class MyQHeaderView : public QHeaderView

{

       Q_OBJECT

public:

       explicit MyQHeaderView(Qt::Orientation orientation, QWidget *parent = 0);

signals:

       void headCheckBoxToggled(bool checked);

 

protected:

       void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const;

       void mousePressEvent(QMouseEvent *event);

       void mouseMoveEvent(QMouseEvent *event);

 

private:

       bool m_isOn;//是否选中

       QPoint m_mousePoint;//鼠标位置

       mutable QRect m_RectHeaderCheckBox;//复选框的位置

};

源文件

#include "QMyHeaderView.h"

 

MyQHeaderView::MyQHeaderView(Qt::Orientation orientation, QWidget *parent)

       : QHeaderView(orientation, parent)

       , m_isOn(false)

{

       m_isOn = false;

       m_mousePoint.setX(100);

       m_mousePoint.setY(100);

}

 

/*绘制表头logicalIndex表示表头第几列,0表示第0列,也就是我们要画复选框的列,rect是表头第一列的大小*/

void MyQHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const

{

       painter->save();

       QHeaderView::paintSection(painter, rect, logicalIndex);

       painter->restore();

       if (logicalIndex == 0)//第一列

       {

              //保存第一列的位置

              m_RectHeaderCheckBox.setX(rect.x() + 3);

              m_RectHeaderCheckBox.setY(rect.y() + 10);

              m_RectHeaderCheckBox.setWidth(14);

              m_RectHeaderCheckBox.setHeight(14);    

              QStyleOptionButton option;

              QPixmap pix;

              if (m_isOn)

              {//图片需要在资源里添加

                     pix.load(":/checkImage/image/checkImage/check-sel.png");//选中

                     option.state = QStyle::State_On;

              }

              else if (m_RectHeaderCheckBox.contains(m_mousePoint))

              {

                     pix.load(":/checkImage/image/checkImage/check-hov.png");//高亮

                     option.state = QStyle::State_HasFocus;

              }

              else

              {

                     pix.load(":/checkImage/image/checkImage/check-nor.png");//正常

                     option.state = QStyle::State_Off;

              }

              style()->drawItemPixmap(painter, rect, Qt::AlignLeft | Qt::AlignVCenter, pix);

              //方法2,画选择按钮,,无法设置背景图片

              //

              //option.rect = m_RectHeaderCheckBox;

              //QIcon icon(pix);

              //option.icon = icon;//setStyleSheet("background");

              //style()->drawControl(QStyle::CE_CheckBox, &option, painter);

       }

}

 

void MyQHeaderView::mousePressEvent(QMouseEvent *event)

{

       //表头可控制,鼠标第一个列,鼠标在矩形范围内

      if (isEnabled() && logicalIndexAt(event->pos()) == 0&&m_RectHeaderCheckBox.contains(event->pos()))

       {

              m_isOn = !m_isOn;

              updateSection(0);//刷新表头第一列

              emit headCheckBoxToggled(m_isOn);

       }

       else QHeaderView::mousePressEvent(event);

}

void MyQHeaderView::mouseMoveEvent(QMouseEvent *event)

{

       //保存鼠标的位置

       m_mousePoint = event->pos();

       if (m_RectHeaderCheckBox.contains(m_mousePoint))

       {

              updateSection(0);//重绘表头复选框

       }

        QHeaderView::mouseMoveEvent(event);

}

(2)   Qdesign中加入QTablewidget,并添加表头字段,第一个为空,用于添加选择复选框。

 

(3)   在主界面类中new一个表头对象,设置到QtableWidget

//new表头对象

MyQHeaderView*myHeader = new MyQHeaderView(Qt::Horizontal, ui.tableWidget);

myHeader->setStretchLastSection(true);

myHeader->setStyleSheet("alignment: left;");

//连接单击信号槽,在槽函数中

QObject::connect(myHeader, &MyQHeaderView::headCheckBoxToggled, this, &AlarmCenter::SetAlarmListCheckState);

ui.tableWidget->setHorizontalHeader(myHeader);//设置表头

(4)   至于QTableWidget表格中数据前面的复选框则是用CheckBox

QCheckBox *CheckBox = new QCheckBox(ui.tableWidget);

QSize size(39, 35);

CheckBox->setFixedSize(size);

CheckBox->setCheckState(Qt::Unchecked);

//创建单击选中的信号槽连接

QObject::connect(CheckBox, &QCheckBox::clicked,this, [=]()

{

       AlarmTableItemChoosed(row, 0);

});

ui.tableWidget->setCellWidget(row, 0, CheckBox);//row行,0列

回帖
  • 消灭零回复
相关主题
2020年最新最新Kubernetes视频教程(K8s)教程 2
程序员转型之制作网课变现,月入过万告别996 1
索鸟快传2.0发布啦 1
两个不同网络的电脑怎么实现文件的互相访问呢? 1
网盘多账号登录软件 1
Java实战闲云旅游项目基于vue+element-ui 1
单点登录技术解决方案基于OAuth2.0的网关鉴权RSA算法生成令牌 1
QT5获取剪贴板上文本信息QT设置剪贴板内容 1
springboot2实战在线购物系统电商系统 1
python web实战之爱家租房项目 1
windows COM实用入门教程 1
C++游戏开发之C++实现的水果忍者游戏 1
计算机视觉库opencv教程 1
node.js实战图书管理系统express框架实现 1
C++实战教程之远程桌面远程控制实战 1
相关主题
PHP7报A non well formed numeric value encountered 0
Linux系统下关闭mongodb的几种命令分享 0
mongodb删除数据、删除集合、删除数据库的命令 0
Git&Github极速入门与攻坚实战课程 0
python爬虫教程使用Django和scrapy实现 0
libnetsnmpmibs.so.31: cannot open shared object file 0
数据结构和算法视频教程 0
redis的hash结构怎么删除数据呢? 0
C++和LUA解析器的数据交互实战视频 0
mongodb errmsg" : "too many users are authenticated 0
C++基础入门视频教程 0
用30个小时精通C++视频教程可能吗? 0
C++分布式多线程游戏服务器开发视频教程socket tcp boost库 0
C++培训教程就业班教程 0
layui的util工具格式时间戳为字符串 0
C++实战教程之远程桌面远程控制实战 1
网络安全培训视频教程 0
LINUX_C++软件工程师视频教程高级项目实战 0
C++高级数据结构与算法视频教程 0
跨域问题很头疼?通过配置nginx轻松解决ajax跨域问题 0