Home > Enterprise >  How do I get a child of a QDialog (access a button) with an enum?
How do I get a child of a QDialog (access a button) with an enum?

Time:09-17

I've got a button, assigned to a QDialog this way:

QPushButton *button = ui->buttonBox->button(QDialogButtonBox::StandardButton::Ok);

How do I get it as a child from a QDialog?

I wanted to use:

parentWidget->findChild<QPushButton*>(QDialogButtonBox::StandardButton::Ok);

but findChild<T> needs a QString.

Any suggestions?

CodePudding user response:

findChild finds a Qt Object by objectname. You have to give your button an Object name first using:

setObjectName(const QString &name)

See documentation here.

CodePudding user response:

Get the buttonBox by its object name first, and then you can get the button you want:

QDialogButtonBox* buttonBox = dialog.findChild<QDialogButtonBox*>("buttonBox");
if (buttonBox)
{
    QPushButton* btn = buttonBox->button(QDialogButtonBox::Ok);
    if (btn)
    {
        qDebug() << "Find it!";
    }
}
  • Related