Qt的QTableWidget如何在表头增加复选框

13 篇文章 2 订阅

QTableWidget只能对表格中的单元格设置复选框,而且只能进行比较有限的控制,如果需要设置图标,显示居中等等,可能需要自定义Item或可以利用setCellWidget将单元格的控件设置为自定义控件,而表头如果是使用自带的表头则无法通过设置显示出复选框,必须进行自定义表头,自定义表头显示复选框有两种方法,具体如下。

1.采用绘制图片的方式在表头绘制复选框

核心代码如下

class CheckBoxHeaderView : public QHeaderView
{
    Q_OBJECT
public:
    CheckBoxHeaderView(int checkColumnIndex, Qt::Orientation orientation, QWidget*                                                         parent = 0) : QHeaderView(orientation, parent)
    {
        m_checkColIndex = checkColumnIndex;
        m_iChecked = Qt::UnChecked;
    }
    void setCheckState(int state)
    {
        m_iChecked = state;
        this->updateSection(m_checkColIndex);
    }
protected:
    void paintSection(QPainter* painter, const QRect &rect, int logicalIndex) const
    {
        painter->save();
        QHeaderView::paintSection(painter, rect, logicalIndex);
        painter->restore();
        if(logicalIndex == m_checkColIndex)
        {
            int width = 10;
            for(int i = 0; i < logicalIndex; i++)
            {
                width += sectionSize(i);
            }
            Qpixmap pix;
            if(Qt::Checked == m_iChecked)
            {
                ///<pix加载选中时的图片
            }
            else if(Qt::Unchecked == m_iChecked)
            {
                ///<pix加载未选中时的图片
            }
            else if(Qt::PartiallyChecked == m_iChecked)
            {
                ///<pix加载部分选中时的图片
            }
            this->style()->drawItemPixmap(painter, rect, Qt::AlignHCenter |             Qt::AlignVCenter, pix);
        }
    }
    void mousePressEvent(QMouseEvent* event)
    {
        if(visualIndexAt(event->pos().x()) == m_checkColIndex)
        {
            if(Qt::PartiallyChecked == m_iChecked || Qt::Unchecked == m_iChecked)
            {
                m_iChecked = Qt::Checked;
            }
            else if(Qt::Checked == m_iChecked)
            {
                m_iChecked = Qt::Unchecked;
            }
            this->updateSection(m_iChecked);
            emit checkStatusChange(m_iChecked );
        }
        QHeaderView::mousePressEvent(event);
    }
signals:
    void checkStatusChange(int);
protected:
    int m_checkColIndex;
    int m_iChecked; 
};

2.采用绘制控件的方式显示复选框

核心代码如下:

class CheckBoxHeaderView : public QHeaderView
{
    Q_OBJECT
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_iChecked = Qt::UnChecked;
        m_pCheckBox = new QCheckBox(); 
    }
    void setCheckState(int state)
    {
        m_iChecked = state;
        this->updateSection(m_checkColIndex);
    }
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(Qt::Checked == m_iChecked)
            {
                option.state = QStyle::State_On;
            }
            else if(Qt::Unchecked == m_iChecked)
            {
                option.state = QStyle::State_Off;
            }
            else if(Qt::PartiallyChecked == m_iChecked)
            {
                option.state = QStyle::State_NoChange;
            }
            if(nullptr != m_pCheckBox)
            {
                m_pcheckBox->setVisible(false);
                m_pCheckBox->setCheckState((Qt::CheckState)m_iChecked);
                this->style()->drawControl(Qstyle::CE_CheckBox, &option, painter, m_pCheckBox);
            }
        }
    }
    void mousePressEvent(QMouseEvent* event)
    {
        if(visualIndexAt(event->pos().x()) == m_checkColIndex)
        {
            if(Qt::PartiallyChecked == m_iChecked || Qt::Unchecked == m_iChecked)
            {
                m_iChecked = Qt::Checked;
            }
            else if(Qt::Checked == m_iChecked)
            {
                m_iChecked = Qt::Unchecked;
            }
            this->updateSection(m_iChecked);
            emit checkStatusChange(m_iChecked );
        }
        QHeaderView::mousePressEvent(event);
    }
signals:
    void checkStatusChange(int);
protected:
    int m_checkColIndex;
    int m_iChecked; 
    QPoint m_topLeft;
    QSize m_checkSize;
    QCheckBox* m_pCheckBox = nullptr;
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值