qt qtablewidget 表头添加多选框,示例 qtablewighet相关问题
#ifndef SCHECKBOXHEADERVIEW_H #define SCHECKBOXHEADERVIEW_H #include <QtGui> #include <QPainter> #include <QHeaderView> #include <QStyleOptionButton> #include <QStyle> class SCheckBoxHeaderView : public QHeaderView { Q_OBJECT private: bool isChecked; int m_checkColIdx; public: SCheckBoxHeaderView( int checkColumnIndex, Qt::Orientation orientation, QWidget * parent = 0) : QHeaderView(orientation, parent) { m_checkColIdx = checkColumnIndex; isChecked = false; } signals: void checkStausChange(bool); protected: void 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(3, 5, 21, 21); if (isChecked) option.state = QStyle::State_On; else option.state = QStyle::State_Off; this->style()->drawControl(QStyle::CE_CheckBox, &option, painter); } } void mousePressEvent(QMouseEvent *event) { if (visualIndexAt(event->pos().x()) == m_checkColIdx) { isChecked = !isChecked; this->updateSection(m_checkColIdx); emit checkStausChange(isChecked); } QHeaderView::mousePressEvent(event); } }; #endif // SCHECKBOXHEADERVIEW_H /* * demo SCheckBoxHeaderView* m_checkHeader = new SCheckBoxHeaderView(0, Qt::Horizontal, this); ui->table_protocolCANdata->setHorizontalHeader(m_checkHeader);// 这个this指针的父为QTableWidget connect(m_checkHeader, &SCheckBoxHeaderView::checkStausChange, [=](bool check){ qDebug() << "is:" <<check; if(check){ for(int i=0;i<ui->table_protocolCANdata->rowCount();i++){ ui->table_protocolCANdata->item(i,0)->setCheckState(Qt::Checked);; } } else{ for(int i=0;i<ui->table_protocolCANdata->rowCount();i++){ ui->table_protocolCANdata->item(i,0)->setCheckState(Qt::Unchecked);; } } }); QTableWidget *table = ui->table_protocolCANdata; table->clearContents(); table->setRowCount(0); table->clear(); QStringList tableHead; tableHead<<" "<< QString("time")<<QString("T")<<QString("P")<<QString("I")<<QString("D"); table->setColumnCount(tableHead.count()); table->setHorizontalHeaderLabels(tableHead); //table->verticalHeader()->setHidden(true);//隐藏列序号 //表格通用操作,比如列宽,哪一列可以编辑 table->setFont(QFont("Helvetica [Cronyx]", 13)); //table->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式 table->setSelectionMode(QAbstractItemView::SingleSelection); //一次只能选中一行 //table->horizontalHeader()->setSectionResizeMode( QHeaderView::Fixed ); table->verticalHeader()->setSectionResizeMode( QHeaderView::Fixed ); table->horizontalHeader()->setMinimumHeight(30); table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); //x先自适应宽度 //table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents); //然后设置要根据内容使用宽度的列 table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed ); table->setColumnWidth(0,20);//第一列设置宽度 //数据 //清空以前的数据 table->clearContents(); table->setRowCount(0); for(int i=0;i<10;i++){ table->setRowCount(table->rowCount()+1); QTableWidgetItem *check=new QTableWidgetItem(""); check->setCheckState(Qt::Unchecked); table->setItem(table->rowCount()-1,0,check); for(int j=1;j<tableHead.count();j++){ QString res = tableHead.at(j); QTableWidgetItem *check=new QTableWidgetItem(res); //设置只读 check->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled); table->setItem(table->rowCount()-1,j,check); } } */
//
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构