Qt表格添加复选框

该方法重写QHeaderView,在表格第一个位置绘制一个复选框,废话不多说,直接上代码,代码较为简单,注释内容足以理解。

#ifndef AICHECKBOXHEADER_H
#define AICHECKBOXHEADER_H

#include<QtWidgets>

class AiCheckBoxHeader : public QHeaderView
{
    Q_OBJECT
public:
    AiCheckBoxHeader(Qt::Orientation orientation, QWidget * parent = 0);
    bool boxIsChecked();//判断复选框是否被选中
    void setChecked(bool checked);//设置复选框的状态

signals:
    void signalStateTrange(bool isChecked);	//复选框状态改变信号

protected:
    void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const;	//绘制复选框
    void mousePressEvent(QMouseEvent *event);	//重写鼠标按下方法,改变复选框状态
    void mouseDoubleClickEvent(QMouseEvent *event);	//增加对鼠标双击事件处理,增加对复选框状态改变的响应速度

private:
    bool isChecked;	//记录复选框的状态


};

#endif // AICHECKBOXHEADER_H

#include "aicheckboxheader.h"

AiCheckBoxHeader::AiCheckBoxHeader(Qt::Orientation orientation, QWidget *parent)
    : QHeaderView(orientation, parent)
{
    isChecked = false;
}

/*
 *@brief:      获取复选框的状态
 *@return:     返回复选框的状态
 */
bool AiCheckBoxHeader::boxIsChecked()
{
    return isChecked;
}

/*
 *@brief:      外部设置复选框的状态
 *@param:      checked:复选框的状态
 */
void AiCheckBoxHeader::setChecked(bool checked)
{
    //如果设置复选框的状态与当前复选框状态不一致时
    if( checked != isChecked ) {
        //由于这个复选框是绘制出来的,所以通过模拟鼠标单击来改变复选框的状态
        QMouseEvent *event = new QMouseEvent(QEvent::MouseButtonPress,QPointF(0,0),Qt::LeftButton,Qt::NoButton,Qt::NoModifier);
        mousePressEvent(event);
    } else {    //一致时,发送个信号给外部使用
        emit signalStateTrange(isChecked);
    }
}

/*
 *@brief:      绘制复选框
 *@date:       2018.09.12
 */
void AiCheckBoxHeader::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
    painter->save();
    QHeaderView::paintSection(painter, rect, logicalIndex);
    painter->restore();
    if( logicalIndex == 0 ) {	//表头第一个位置绘制复选框
        QStyleOptionButton option;
        option.rect = QRect(10, 10, 10, 10);
        if ( isChecked ) {
            option.state = QStyle::State_On;
        } else {
            option.state = QStyle::State_Off;
        }
        QCheckBox checkBox;
        option.iconSize = QSize(20, 20);
        option.rect = rect;

        style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter, &checkBox);
    }
}

/*
 *@brief:      鼠标按下事件,改变复选框的状态
 *@date:       2018.09.12
 */
void AiCheckBoxHeader::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        int index = logicalIndexAt(event->pos());
        if( index == 0 ) {
            if( isChecked ) {
                isChecked = false;
            } else {
                isChecked = true;
            }
            emit signalStateTrange(isChecked);
        }
    }
    this->update();
    QHeaderView::mousePressEvent(event);
}

/*
 *@brief:      鼠标双击事件,为提高复选框响应频率
 *@date:       2018.09.12
 */
void AiCheckBoxHeader::mouseDoubleClickEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        //获取点击表头的标签号
        int index = logicalIndexAt(event->pos());
        if( index == 0 ) {
            if( isChecked ) {
                isChecked = false;
            } else {
                isChecked = true;
            }
            emit signalStateTrange(isChecked);
        }
    }
    this->update();
    QHeaderView::mouseDoubleClickEvent(event);
}

使用方法如下:

//首先定义一个复选框表头
AiCheckBoxHeader *updateHeader = new AiCheckBoxHeader(Qt::Horizontal,updateTable);
//其次设置表格的表头
updateTable->setHorizontalHeader(updateHeader);

这种设置复选框优点在于复选框样式支持qss设置,设置QCheckBox的样式即可。

发布了11 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/aicamel/article/details/85262971
今日推荐