0

私はで使用QCheckBoxします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);

私はこれを得ることができません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());
4

3 に答える 3

2

次のことを行う必要があると思います。

QCheckBox *chkBox = qobject_cast<QCheckBox*>(_ui->tableBonus1Lines->cellWidget(0, 0));
于 2014-10-09T14:57:48.687 に答える
1

CheckBoxこのコードのヘルプで中央揃えを使用して取得できます。

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;
}
于 2016-03-29T07:14:34.053 に答える