简体   繁体   中英

How to extract widgets from QHBoxLayout in Qt

I have a TableWidget into which I add Widgets like this:

QLabel *l = new QLabel("TEST");
QWidget *widget = new QWidget();
QHBoxLayout *hbox = new QHBoxLayout();

hbox->addWidget(l);
hbox->setAlignment(Qt::AlignCenter);
hbox->setContentsMargins(0,0,0,0);
widget->setLayout(hbox);

ui->tableWidget->setCellWidget(0, 0, widget);

When a cell gets double clicked I capture the event and would like to figure out what QLabel it is.

But how do I extract it again or do I even have to?

auto    widget = ui->tableWidget->cellWidget(ui->tableWidget->currentRow(), ui->tableWidget->currentColumn()); // if mode is SingleSelection
auto    hbox = widget->layout();
auto    label = qobject_cast<QLabel *>(hbox->itemAt(0)->widget());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM