将QTableWidgetItem从cellWidget()的QCheckBox中取出

时间:2016-07-16 21:47:05

标签: qt qtablewidget qtablewidgetitem qcheckbox

我将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()的{​​{1}}:

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 - 表格中的行数。但是,他们的孩子只有QWidgetsQHBoxLayout。显然没有QCheckBox的引用 - 看起来我是在某个并行对象层次结构中,QTableWidgetItem在其他层次结构中。真的吗?如何获得该项目?

1 个答案:

答案 0 :(得分:0)

请参阅此问题:How to work with signals from QTableWidget cell with cellWidget set

适应你的情况:

void MainWindow::checkBoxStateChanged(int)
{
    QCheckBox * box = qobject_cast< QCheckBox * >( sender() );
    if (box)
    {
        int row = box->property("row").toInt();
        int column = box->property("column").toInt();
        QTableWidgetItem* item = tableWidget->item(row, column);
    }
}