1

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?

Mykola Tetiuk
  • 157
  • 3
  • 9

2 Answers2

2

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!";
    }
}
Ye.Feng
  • 709
  • 6
  • 9
2

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.

Mailerdaimon
  • 6,003
  • 3
  • 35
  • 46