QT重写表头之表头绘制复选框并且增加鼠标点击事件

#include "myheaderviewer.h"
#include<QPainter>
#include<QMouseEvent>

void MyHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
    painter->save();
    QHeaderView::paintSection(painter, rect, logicalIndex); //父类继承否则表头默认部分失效
    painter->restore();
    if (logicalIndex == 1) //表头第一列
    {
    	//绘制大小,根据类成员triState1 状态来改变复选框的状态
        QStyleOptionButton option;
        option.initFrom(this);
        option.rect = QRect(rect.x()/* + 10*/, rect.y() + 10, 18, 18);
        option.features = QStyleOptionButton::None;
        if (triState1 == Qt::Checked)
            option.state |= QStyle::State_On;
        else if (triState1 == Qt::PartiallyChecked)
            option.state |= QStyle::State_NoChange;
        else
            option.state |= QStyle::State_Off;
        style()->drawControl(QStyle::CE_CheckBox, &option, painter/*, checkBox*/);
    }
}
//通过事件点击改变复选框的状态
void MyHeaderView::mousePressEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton) {  //判断鼠标左键
        if (logicalIndexAt(event->pos()) == 1) { 判断序列号第一个的位置
            switch (triState1) {
            case Qt::Unchecked:
                triState1 = Qt::Checked;
                break;
            case Qt::Checked:
                triState1 = Qt::Unchecked;
                break;
            }
//            updateSection(1);
        }
        emit checkBoxChangedIndex(logicalIndexAt(event->pos())); //点击复选框并且出发对外信号
    }
    QHeaderView::mousePressEvent(event);
}
  • 22
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值