posts - 10,comments - 1,views - 23577

QTableWidget添加单选框分为表头添加单选框和表格中添加单选框,下面分别进行讨论:

QTableWidget表格中添加单选框(并且让单选框居中)

复制代码
//设置操作框并设置为居中
QWidget *widget = new QWidget;
QHBoxLayout *layout = new QHBoxLayout;
QCheckBox *box = new QCheckBox;
layout->addWidget(box);
layout->setMargin(0);
layout->setAlignment(box, Qt::AlignCenter);
widget->setLayout(layout);
table->setCellWidget(row, column, widget);
复制代码

表头添加单选框需要继承QCheckBoxHeaderView类重写部分内容才可以,下面是示例代码:

复制代码
#pragma once
#include <QtGui>
#include <QHeaderView>
#include <QStyleOptionButton>
#include <QStyle>

class CCheckBoxHeaderView : public QHeaderView
{
    Q_OBJECT
public:
    CCheckBoxHeaderView(int checkColumnIndex,Qt::Orientation orientation,QWidget * parent = 0);
    void setCheckStaus(bool isChecked);
    bool getCheckStaus();
signals:
    void checkStausChange(bool);

protected:
    void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const;
    void mousePressEvent(QMouseEvent *event);

private:
    bool m_isChecked;
    int m_checkColIdx;
};
复制代码
复制代码
#include "CCheckBoxHeaderView.h"

CCheckBoxHeaderView::CCheckBoxHeaderView(int checkColumnIndex,Qt::Orientation orientation,QWidget * parent)
    :QHeaderView(orientation, parent)
{
    m_checkColIdx = checkColumnIndex;
    m_isChecked = false;
}

void CCheckBoxHeaderView::setCheckStaus(bool isChecked)
{
    m_isChecked = isChecked;
    this->updateSection(m_checkColIdx);
}

bool CCheckBoxHeaderView::getCheckStaus()
{
    return m_isChecked;
}

void CCheckBoxHeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
    painter->save();
    QHeaderView::paintSection(painter, rect, logicalIndex);
    painter->restore();
    if (logicalIndex == m_checkColIdx)
    {
        QStyleOptionButton option;
        int width = 10;
        for (int i = 0; i < logicalIndex; ++i)
            width += sectionSize(i);
        option.rect = QRect(5, 5, 21, 21);
        if (m_isChecked)
            option.state = QStyle::State_On;
        else
            option.state = QStyle::State_Off;
        //        this->style()->drawPrimitive(QStyle::PE_IndicatorCheckBox, &option, painter);
        this->style()->drawControl(QStyle::CE_CheckBox, &option, painter);
    }
}
void CCheckBoxHeaderView::mousePressEvent(QMouseEvent *event)
{
    if (visualIndexAt(event->pos().x()) == m_checkColIdx)
    {
        if (m_isChecked)
            m_isChecked = false;
        else
            m_isChecked = true;
        this->updateSection(m_checkColIdx);
        emit checkStausChange(m_isChecked);
    }
    QHeaderView::mousePressEvent(event);
}
复制代码

最后在类中还需要添加表头并设置对应槽函数:

复制代码
//设置表头全选框
    CCheckBoxHeaderView *myHeader = new CCheckBoxHeaderView(0, Qt::Horizontal, ui.tableWidget);
    ui.tableWidget->setHorizontalHeader(myHeader);
    //点击全选框后
    connect(myHeader, &CCheckBoxHeaderView::checkStausChange, [&](bool flag) {
        Qt::CheckState state = (flag ? Qt::Checked : Qt::Unchecked);
        for (int index = 0;index < ui.tableWidget->rowCount();index++)
        {
            QWidget *widget = (QWidget *)ui.tableWidget->cellWidget(index, 0);
            QCheckBox *ckb = (QCheckBox *)widget->children().at(1);
            ckb->setCheckState(state);
        }
    });
复制代码

最后样式如下图所示:

posted on   青春凹陷  阅读(1704)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示