If you created a widget using something similar:
QWidget* createCheckBoxWidget(bool checked)
{
QWidget* pWidget = new QWidget();
QCheckBox* pCheckBox = new QCheckBox();
pCheckBox->setChecked(checked);
QHBoxLayout* pLayout = new QHBoxLayout(pWidget);
pLayout->addWidget(pCheckBox);
pLayout->setAlignment(Qt::AlignCenter);
pLayout->setContentsMargins(0,0,0,0);
pWidget->setLayout(pLayout);
return pWidget;
}
Then added it to a QTableWidget like so:
QTableWidget* tableWidget = new QTableWidget();
tableWidget->setRowCount(1);
tableWidget->setColumnCount(1);
QWidget* checkBox = createCheckBoxWidget(true);
tableWidget->setCellWidget(0, 0, checkBox);
You could retrieve it using the following function:
QCheckBox* getCheckBoxWidgetFromCell(QTableWidget* table, int row, int col)
{
QCheckBox* checkBox = nullptr;
if (QWidget* w = table->cellWidget(row, col))
{
if (QLayout* layout = w->layout())
{
if (QLayoutItem* layoutItem = layout->itemAt(0))
{
if (QWidgetItem* widgetItem = dynamic_cast<QWidgetItem*>(layoutItem))
{
checkBox = qobject_cast<QCheckBox*>(widgetItem->widget());
}
}
}
}
return checkBox;
}
And access its state like so:
QCheckBox* checkBox = getCheckBoxWidgetFromCell(tableWidget, 0, 0);
if (checkBox)
{
bool checked = checkBox->isChecked();
}
So it's important to know the hierarchy of the objects you've inserted into a table's cell.
The layout here is optional, but does provide you control with how your widget is displayed inside the cell. It also shows that a cell can contain very complex widgets or groups of widgets as needed.