Ufx
Ufx

Reputation: 2695

How to get widget from QTableWidgetCell

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());

Upvotes: 0

Views: 1732

Answers (3)

Pascal Laferri&#232;re
Pascal Laferri&#232;re

Reputation: 109

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.

Upvotes: 1

bugtsa
bugtsa

Reputation: 361

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;
}

Upvotes: 1

vahancho
vahancho

Reputation: 21248

I think you need to do the following:

QCheckBox *chkBox = qobject_cast<QCheckBox*>(_ui->tableBonus1Lines->cellWidget(0, 0));

Upvotes: 2

Related Questions