当前位置:   article > 正文

QTableWidget表头加入QCheckBox(复选框表头)_qtablewidget 设置勾选框

qtablewidget 设置勾选框

最近发现在QTableWidget的表头中加入复选框这个需求较为普遍,网上相关的资料却很少,实现此功能所需相关的Qt方法也较为冷门,特作此分享。

主要实现方法为重写QHeaderView类的*void paintSection(QPainter painter, const QRect &rect, int logicalIndex) const; 函数和 *void mousePressEvent(QMouseEvent event); 函数,下面直接贴完整代码和示例,方便大家使用。

1.头文件如下:

#ifndef CHECKBOXHEADERVIEW_H
#define CHECKBOXHEADERVIEW_H

#include <QtGui>
#include <QPainter>
#include <QHeaderView>
#include <QStyleOptionButton>
#include <QStyle>
#include <QCheckBox>
#include <QEvent>

class CheckBoxHeaderView : public QHeaderView
{
    Q_OBJECT

public:
    CheckBoxHeaderView(int checkColumnIndex, QPoint topLeft, QSize size, 
    Qt::Orientation orientation, QWidget *parent = nullptr);

protected:
    void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const;
    void mousePressEvent(QMouseEvent *event);

private:
    int     checkColIndex;  //表头列下标
    QPoint  topLeft;        //勾选框起始屏幕坐标
    QSize   boxSize;        //勾选框大小
    bool    isChecked;      //勾选框状态

public:
    void setCheckState(bool state);             //设置复选框状态

signals:
    void signalCheckStateChanged(bool state);   //勾选状态发生改变信号
};

#endif // CHECKBOXHEADERVIEW_H

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

2.cpp文件如下:

#include "checkboxheaderview.h"

CheckBoxHeaderView::CheckBoxHeaderView(int checkColumnIndex, QPoint topLeft, QSize size, Qt::Orientation orientation, QWidget *parent) : QHeaderView(orientation, parent)
{
    checkColIndex = checkColumnIndex;
    this->topLeft = topLeft;
    boxSize = size;
    isChecked = false;
}

void CheckBoxHeaderView::setCheckState(bool state)
{
    isChecked = state;
}


void CheckBoxHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
    painter->save();
    QHeaderView::paintSection(painter, rect, logicalIndex);
    painter->restore();
    if (logicalIndex == checkColIndex)
    {
        QStyleOptionButton option;
        option.rect = QRect(topLeft.x(), topLeft.y(), boxSize.width(), boxSize.height());
        if (isChecked)
        {
            option.state = QStyle::State_On;
        }
        else
        {
            option.state = QStyle::State_Off;
        }
        //加入复选框,设置样式
        QCheckBox *check = new QCheckBox;
        QString sheet = QString("QCheckBox::indicator {width: %1px;  height: %2px;}").arg(boxSize.width()).arg(boxSize.height());
        check->setStyleSheet(sheet);
        this->style()->drawControl(QStyle::CE_CheckBox, &option, painter, check);
    }
}

void CheckBoxHeaderView::mousePressEvent(QMouseEvent *event)
{
    if (visualIndexAt(event->pos().x()) == checkColIndex)
    {
        isChecked = !isChecked;
        this->updateSection(checkColIndex);
        emit signalCheckStateChanged(isChecked);
    }
    //继承后此信号必须手动发送,否则无法响应
    emit QHeaderView::sectionClicked(visualIndexAt(event->pos().x()));
    QHeaderView::mousePressEvent(event);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

3.使用示例:

    CheckBoxHeaderView *header = new CheckBoxHeaderView(0, QPoint(10, 5), QSize(20, 20), Qt::Horizontal, this);
    ui->tableWidget->setColumnCount(2);
    ui->tableWidget->setHorizontalHeaderLabels(QStringList() << "复选框" << "无复选框");
    ui->tableWidget->setHorizontalHeader(header);

    connect(header, &CheckBoxHeaderView::signalCheckStateChanged, [=](bool state)
    {
        qDebug() << "勾选状态:" << state;
    });
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

效果图:
在这里插入图片描述
什么?!你说懒得复制粘贴?OKOK,我上传demo,设置0积分,csdn系统会自动改积分,有积分的就直接下载,没积分的再@我。
点击这里去下载demo

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/282940
推荐阅读
相关标签
  

闽ICP备14008679号