0

I use QCheckBox in QTableWidgetCell

QWidget *widget = new QWidget();
QCheckBox *checkBox = new QCheckBox();
QHBoxLayout *layout = new QHBoxLayout(widget);
layout->addWidget(checkBox);
layout->setAlignment(Qt::AlignCenter);
layout->setContentsMargins(0, 0, 0, 0);
widget->setLayout(layout);
table->setCellWidget(0, 0, widget);

I cannot get this QCheckBox

QTableWidgetItem *item     = ui->table->item(0, 0);

QWidget          *widget   = dynamic_cast<QWidget *>(item); // Widget==0

QHBoxLayout      *layout   = dynamic_cast<QHBoxLayout *>(widget->layout());
QCheckBox        *checkBox = dynamic_cast<QCheckBox *>(layout->widget());
Ufx
  • 2,595
  • 12
  • 44
  • 83

3 Answers3

2

I think you need to do the following:

QCheckBox *chkBox = qobject_cast<QCheckBox*>(_ui->tableBonus1Lines->cellWidget(0, 0));
vahancho
  • 20,808
  • 3
  • 47
  • 55
  • @Ufx, that is because you set the cell widget the `QWidget` that contains a `QCheckBox`. Why don't you add the check box directly without wrapping it by another `QWidget`? – vahancho Oct 09 '14 at 15:13
  • I just tried to use `QCheckBox *checkBox = new QCheckBox(); table->setCellWidget(0, 0, checkBox);`. Does not work. chkBox is 0. – Ufx Oct 09 '14 at 15:27
1

You can get CheckBox with center alignment at help this code:

try {
    QWidget *mainWidget = qobject_cast<QWidget *>(pTableWidget->cellWidget(row, column);
    QHBoxLayout *hBoxLayout = qobject_cast<QHBoxLayout *>(mainWidget->layout());
    QLayoutItem *item = hBoxLayout->layout()->takeAt(0);
    QWidget* widget = item->widget();
    QCheckBox *chechBox = qobject_cast<QCheckBox *>(widget);
    return chechBox;
} catch (...) {
    return NULL;
}
unicorn2
  • 844
  • 13
  • 30
bugtsa
  • 361
  • 3
  • 2
1

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.