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的樣式即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章