Recently I found the way that checkbox places in the middle of QtableWidget item. However, I do not know how to check state whether or not button is clicked. Could you tell me how to check button state?
here is what Ive found code:
QWidget *pWidget = new QWidget();
QCheckBox *pCheckBox = new QCheckBox();
QHBoxLayout *pLayout = new QHBoxLayout(pWidget);
pCheckBox->setCheckState(Qt::Checked);
pLayout->addWidget(pCheckBox);
pLayout->setAlignment(Qt::AlignCenter);
pLayout->setContentsMargins(0,0,0,0);
pWidget->setLayout(pLayout);
ui->tableWidget2->setCellWidget(2,2, pWidget);
Although this is very late you can solve it like this:
This works for other types as well (QComboBox etc.). Although it would probably be better to just use the checkbox functionality that
QTableWidgetItemalready has.This example might not work if you are using a tristate checkbox in which case you should call:
checkState()and compare it toQt::CheckState. Ifqobject_cast<T>does not work out you can use areinterpret_cast<T>.