QCheckBox * checkB = new QCheckBox(this); //创建checkbox
checkB->setCheckState(Qt::Checked); //设置状态
QWidget *w = new QWidget(this); //创建一个widget
QHBoxLayout *hLayout = new QHBoxLayout(); //创建布局
hLayout->addWidget(checkB); //添加checkbox
hLayout->setMargin(0); //设置边缘距离 否则会很难看
hLayout->setAlignment(checkB, Qt::AlignCenter); //居中
w->setLayout(hLayout); //设置widget的布局
ui->tableWidget_UnrecognizedPrimitive->setCellWidget(row, column, w); //将widget放到table中
//采取下面方式获取
if (QWidget *w = ui->tableWidget_IdentifiedPrimitives->cellWidget(row, column))//先获取widget
{
QCheckBox * checkBox = qobject_cast<QCheckBox*>(w->children().at(1)); //通过children来访问checkbox
if ( checkBox )
{
if ( checkBox->checkState() == Qt::Unchecked ) //未选中
{
//操作
}
}
}