QTableView设置表头复选框居中

1、说明

A、表头复选框实现三态复选框显示效果。

B、自定义信号void checkStateChanged(Qt::CheckState state); 表示复选框状态变化。

2、显示效果

实现单元格复选框居中请参照

QTableView 中设置复选框列居中_qtableview居中-CSDN博客

3、代码使用

    QCheckBoxHeaderView* checkBoxHeaderView  = new QCheckBoxHeaderView(0, Qt::Horizontal,ui->tableView);
    ui->tableView->setHorizontalHeader(checkBoxHeaderView);
    connect(checkBoxHeaderView, SIGNAL(checkStateChanged(Qt::CheckState)), this, SLOT(slotC

4、源代码 

#ifndef QCHECKBOXHEADERVIEW_H
#define QCHECKBOXHEADERVIEW_H

#include <QObject>
#include <QHeaderView>
#include <QPainter>

class QCheckBoxHeaderView : public QHeaderView
{
    Q_OBJECT
public:
    QCheckBoxHeaderView(int nCheckColIndex, Qt::Orientation orientation, QWidget *parent = nullptr);
    void SetCheckState(Qt::CheckState state);
protected:
    void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const override;
    void mousePressEvent(QMouseEvent *event) override;

signals:
    void checkStateChanged(Qt::CheckState state);
private:
    int m_nCheckColIndex;       //复选框列下标
    Qt::CheckState m_checkState;
};

#endif // QCHECKBOXHEADERVIEW_H
#include "QCheckBoxHeaderView.h"
#include <QMouseEvent>
#include <QApplication>
#include <QPainter>
#include <QStyle>

QCheckBoxHeaderView::QCheckBoxHeaderView(int nCheckColIndex, Qt::Orientation orientation, QWidget *parent):
    QHeaderView (orientation, parent),m_nCheckColIndex(nCheckColIndex)
{
    setSectionsClickable(true);
    m_checkState = Qt::Unchecked;
}

void QCheckBoxHeaderView::SetCheckState(Qt::CheckState state)
{
    m_checkState = state;
    updateSection(m_nCheckColIndex);
}

void QCheckBoxHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
    painter->save();
    QHeaderView::paintSection(painter, rect, logicalIndex);
    painter->restore();


    if(m_nCheckColIndex == logicalIndex)
    {
        QStyleOptionButton option;
        option.initFrom(this);
        QPoint centerPt = rect.center();
        option.rect = QRect(centerPt.x()-6, centerPt.y()-10, 19, 19);
        option.features = QStyleOptionButton::None;
        if(m_checkState == Qt::Checked)
        {
            option.state |= QStyle::State_On;;
        }
        else if(m_checkState == Qt::Unchecked)
        {
            option.state |= QStyle::State_Off;
        }
        else
        {
            option.state |= QStyle::State_NoChange;
        }
        style()->drawControl(QStyle::CE_CheckBox, &option, painter);
    }

}

void QCheckBoxHeaderView::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        if(logicalIndexAt(event->pos()) == m_nCheckColIndex)
        {
            int xPos = sectionViewportPosition(m_nCheckColIndex);
            int width = sectionSize(m_nCheckColIndex);
            QRect sectionRect(xPos, 0, width, height());
            QStyleOptionButton checkOption;
            QRect checkBoxRect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator, &checkOption, nullptr);
            checkBoxRect.moveCenter(sectionRect.center());
            if(checkBoxRect.contains(event->pos()))
            {
                if(m_checkState == Qt::Checked)
                {
                    m_checkState = Qt::Unchecked;
                }
                else if(m_checkState == Qt::Unchecked)
                {
                    m_checkState = Qt::Checked;
                }
                else
                {
                    m_checkState = Qt::Checked;
                }
                updateSection(m_nCheckColIndex);
                emit checkStateChanged(m_checkState);
            }
        }
    }
    QHeaderView::mousePressEvent(event);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值