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); } });
最后样式如下图所示:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具