您的位置:首页 > 编程语言 > Qt开发

【Qt】在QTableWidget中添加QCheckBox并使其居中显示【2012-11-21更新】

2012-11-21 16:41 387 查看
实现思路:把QCheckBox嵌入式到一个水平布局中

QWidget *widget;
QHBoxLayout *hLayout;
QCheckBox *ckb;
...
ckb = new QCheckBox();
hLayout = new QHBoxLayout();
widget = new QWidget(ui->tableWidget);
hLayout->addWidget(ckb);
hLayout->setMargin(0);                          // 必须添加, 否则CheckBox不能正常显示
hLayout->setAlignment(ckb, Qt::AlignCenter);
widget->setLayout(hLayout);
...
ui->tableWidget->setCellWidget(row, column, widget);


获取CheckBox的指针的方法

QWidget *widget = (QWidget *)ui->tableWidget->cellWidget(row, column);
QCheckBox *ckb = (QCheckBox *)widget->children().at(1);
ckb->setChecked(true);
...


备注:
可使用 qDebug() << widget->children(); 输出widget的child列表
从而判断CheckBox的index
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: