QTableWidget表头添加复选框实现全选功能

QTableWidget表头添加复选框实现全选功能

主要是CheckBoxHeaderView继承QHeaderView,重写paintSection函数


    void checkStatusChange(bool);

是复选框是否选中的信号。

详细代码如下:
 

#ifndef CHECKBOXHEADERVIEW_H
#define CHECKBOXHEADERVIEW_H

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

/// 复选框表头

class CheckBoxHeaderView : public QHeaderView
{
    Q_OBJECT
private:
    int     m_checkColIndex;    //列下标
    QPoint  m_topLeft;          //勾选框起始坐标
    QSize   m_checkSize;        //勾选框大小
    bool    m_isChecked;        //勾选框状态
public:
    CheckBoxHeaderView( int checkColumnIndex, QPoint topLeft, QSize size, Qt::Orientation orientation, QWidget * parent = 0) : QHeaderView(orientation, parent)
    {
        m_checkColIndex = checkColumnIndex;
        m_topLeft = topLeft;
        m_checkSize = size;
        m_isChecked = false;
    }

    void setCheckState(bool state)
    {
        m_isChecked = state;
    }

protected:
    void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
    {
        painter->save();
        QHeaderView::paintSection(painter, rect, logicalIndex);
        painter->restore();
        if (logicalIndex == m_checkColIndex)
        {
            QStyleOptionButton option;
            int width = 10;
            for (int i=0; i<logicalIndex; ++i)
            {
                width += sectionSize( i );
            }
            option.rect = QRect(m_topLeft.x(), m_topLeft.y(), m_checkSize.width(), m_checkSize.height());
            if (m_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(m_checkSize.width()).arg(m_checkSize.height());
            check->setStyleSheet(sheet);
            this->style()->drawControl(QStyle::CE_CheckBox, &option, painter, check);
        }
    }

    void mousePressEvent(QMouseEvent *event)
    {
        if (visualIndexAt(event->pos().x()) == m_checkColIndex)
        {
            m_isChecked = !m_isChecked;
            this->updateSection(m_checkColIndex);
            emit checkStatusChange(m_isChecked);
        }
        QHeaderView::mousePressEvent(event);
    }
signals:
    void checkStatusChange(bool);
};

#endif // CHECKBOXHEADERVIEW_H

写个例子:

  m_checkHeaderView = new CheckBoxHeaderView(0, QPoint(26 , 13),QSize(20 , 20 ), Qt::Horizontal, this);
    m_checkHeaderView->setObjectName(QStringLiteral("m_checkHeaderView"));
    ui->tbwReportNumberAdptive->setHorizontalHeader( m_checkHeaderView );

//绑定信号和槽,然后就可以实现全选功能了
    connect(m_checkHeaderView, &CheckBoxHeaderView::checkStatusChange, this, &frmReport::slotCheckStatusChange);

 

  • 1
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值