12

When you open a QMessageBox with detailed text set it has the show details button. I would like the details to be displayed by default, rather than the user having to click on the Show Details... button first.

qt_doc_example

UmNyobe
  • 22,539
  • 9
  • 61
  • 90

3 Answers3

8

As far as I can tell from a quick look through the source, there is is no easy way to directly open the details text, or indeed access the "Show Details..." button. The best method I could find was to:

  1. Iterate through all buttons on the message box.
  2. Extract the one with the role ActionRole, as this corresponds to the "Show Details..." button.
  3. Call the click method manually on this.

A code sample of this in action:

#include <QAbstractButton>
#include <QApplication>
#include <QMessageBox>

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    QMessageBox messageBox;
    messageBox.setText("Some text");
    messageBox.setDetailedText("More details go here");

    // Loop through all buttons, looking for one with the "ActionRole" button
    // role. This is the "Show Details..." button.
    QAbstractButton *detailsButton = NULL;

    foreach (QAbstractButton *button, messageBox.buttons()) {
        if (messageBox.buttonRole(button) == QMessageBox::ActionRole) {
            detailsButton = button;
            break;
        }
    }

    // If we have found the details button, then click it to expand the
    // details area.
    if (detailsButton) {
        detailsButton->click();
    }

    // Show the message box.
    messageBox.exec();

    return app.exec();
}
ajshort
  • 3,684
  • 5
  • 29
  • 43
  • Your example did not work for me. When I iterate through the buttons, it only finds the standard buttons (e.g., "Yes" and "No"). The "Show Details" button was not among them at the time I was crawling through them. Using Qt version 4.7.4. – Mike Finch Jan 05 '18 at 22:51
  • Not sure sorry - from memory I tested this on Qt 5.x. Maybe try looking through each `QAbstractButton` child of the message box using a recursive `findChildren` call? – ajshort Jan 09 '18 at 05:00
  • Yes, I had tried searching for the "Show Details" button via `findChildren()` also. I can indeed find that "Show Details" button in that way, but its role at the time I find it is `InvalidRole`. I called `click()` on that button anyway, just to see what would happen. It had no effect. – Mike Finch Jan 11 '18 at 14:30
  • You can fuse the last if with the loop: ``` QAbstractButton *detailsButton = nullptr; foreach (QAbstractButton *button, messageBox.buttons()) { if (messageBox.buttonRole(button) == QMessageBox::ActionRole) { detailsButton = button; detailsButton->click(); // click it to expand the text break; } } ``` – mmerle Apr 22 '21 at 09:20
4

This function will expand the details by default and also resize the text box to a bigger size:

#include <QTextEdit>
#include <QMessageBox>
#include <QAbstractButton>

void showDetailsInQMessageBox(QMessageBox& messageBox)
{
    foreach (QAbstractButton *button, messageBox.buttons())
    {
        if (messageBox.buttonRole(button) == QMessageBox::ActionRole)
        {
            button->click();
            break;
        }
    }
    QList<QTextEdit*> textBoxes = messageBox.findChildren<QTextEdit*>();
    if(textBoxes.size())
        textBoxes[0]->setFixedSize(750, 250);
}

...  //somewhere else

QMessageBox box;
showDetailsInQMessageBox(box);
Adriel Jr
  • 2,451
  • 19
  • 25
1

On Qt5 at least:

    QMessageBox msgBox;
    msgBox.setText("Some text");
    msgBox.setDetailedText(text);

    // Search the "Show Details..." button
    foreach (QAbstractButton *button, msgBox.buttons())
    {
        if (msgBox.buttonRole(button) == QMessageBox::ActionRole)
        {
            button->click(); // click it to expand the text
            break;
        }
    }

    msgBox.exec();
mmerle
  • 451
  • 4
  • 10