qtqpushbuttonqmessageboxsetdefault

How to set the default button to "Show details"?


I am having this trouble with QMessageBox:

QMessageBox box;
box.setWindowTitle(tr("Warning box"));
box.setIcon(QMessageBox::Warning);
box.setText(tr("Something happened, see details or press OK"));
box.setDetailedText("There is a detail. World is not answering to Hello, try to say Hello world again!");
box.setStandardButtons(QMessageBox::Ok);
box.setDefaultButton(QMessageBox::NoButton);
box.exec();

If I do:

box.setDefaultButton(QMessageBox::NoButton);

the OK button is still marked as default.

I did not find the QMessageBox::ShowDetails enum, so I cannot set as default button the Show details push button.

Is there any way to accomplish this?


Solution

  • I would suggest you to use QMessageBox::setDefaultButton in order to set the first QPushButton, which is a child of your QMessageBox, as default like this:

    if (!box.detailedText().isEmpty())
            box.setDefaultButton(box.findChildren<QPushButton *>().first());