如何在QTableWidget中实现QCheckBox

 我来答
匿名用户
推荐于2016-02-05
展开全部
#include "widget.h"  
#include "ui_widget.h"  
#include <QTableWidgetItem>  
#include <QCheckBox>  
#include <QHBoxLayout>  
#include <QDebug>  
Widget::Widget(QWidget *parent) :  
    QWidget(parent),  
    ui(new Ui::Widget)  
{  
    ui->setupUi(this);  
    /*这是ui文件中没有放qtablewidget控件时在里面插入QCkeckBox的方法*/  
//    QTableWidget *table=new QTableWidget(5,5);  
//    QCheckBox *abc=new QCheckBox("");  
//    table->setCellWidget(0,0,abc);  
//    QHBoxLayout *mainLayout = new QHBoxLayout;  
//    mainLayout->addWidget(table);  
//    setLayout(mainLayout);  
  
    /*这是ui文件中已经放了QtableWieget控件时在里面插入QCheckBox的方法*/  
//    QCheckBox *abc=new QCheckBox("");  
//    ui->tableWidget->setColumnCount(2);  
//    ui->tableWidget->setRowCount(2);  
//    ui->tableWidget ->setCellWidget(0,0,abc);  
  
    /*这是利用QTableWidget自带的属性插入QCheckBox的方法,据说前两中方法不能读取单选框的选择状态(我测试了一下,发现这种说法并不完全对,尽管失败了)而这种可以读取状态的方法是利用QTableWidget::cellChanged()函数,检查单元格内容的变化,然后连接此信号,在槽函数中检测checkBox的状态。 
 
connect(tableWidget, SIGNAL(cellChanged(int,int)), this, SLOT(changeTest(int, int))); 
 
 void changeTest(int row, int col) 
 
 { 
 
     if(tableWidget ->item(row, col)->checkState() == Qt::Checked) //选中 
 
         ... 
 
     else 
 
         ... 
 
 } 
*/ 
QTableWidgetItem *asd=new QTableWidgetItem();
    asd->setCheckState(Qt::Checked);
    ui->tableWidget->setColumnCount(3);
    ui->tableWidget->setRowCount(3);
    ui->tableWidget->setItem(0,0,asd);
}
Widget::~Widget()
{
    delete ui;
}
苏州蓝晓生物科技有限公司_
2022-08-05 广告
苏州蓝晓生物科技有限公司。标准化核心产品:公司拥有完整的琼脂糖介质、葡聚糖介质、聚甲基丙烯酸酯介质生产线,年产分离介质50000L,产品质量稳定并达到国际领先水平。核心优势:公司核心技术人员拥有近二十年不同基质的基球开发和官能化的丰富技术经... 点击进入详情页
本回答由苏州蓝晓生物科技有限公司_提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式