0


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

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;
};
标签: qt 开发语言 ui

本文转载自: https://blog.csdn.net/wangshuping18/article/details/128970853
版权归原作者 随性随笔 所有, 如有侵权,请联系我们删除。

“Qt的QTableWidget如何在表头增加复选框”的评论:

还没有评论