我以以下方式将 QCheckBox 存储在 QTableWidget 中:
QCheckBox *checkBox = new QCheckBox();
QWidget *widget = new QWidget();
QHBoxLayout *layout = new QHBoxLayout(widget);
layout->addWidget(checkBox);
layout->setAlignment(Qt::AlignCenter);
layout->setContentsMargins(0,0,0,0);
widget->setLayout(layout);
tableWidget->setCellWidget(row, 2, widget);
然后,我抓住stateChanged()了checkBox:
connect( checkBox, SIGNAL(stateChanged(int)), this, SLOT(checkBoxStateChanged(int)) );
void MainWindow::checkBoxStateChanged(int)
{
QCheckBox * box = qobject_cast< QCheckBox * >( sender() );
if( !box ) {
return;
}
}
现在,我可以去QTableWidget——它是box->parent()->parent()->parent()。在此之前的对象,即box->parent()->parent(),是qt_scrollarea_viewport(即objectName())。我搜索了“视口”的子项,并且有16 QWidgets- 我的表中的行数。然而,他们的孩子只是QHBoxLayout和QCheckBox。显然没有引用QTableWidgetItem- 看起来我是否在某个并行对象层次结构中,并且QTableWidgetItem在其他层次结构中。真的吗?如何获得物品?