UmNyobe
UmNyobe

Reputation: 22890

QMessageBox "show details"

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

Upvotes: 13

Views: 6260

Answers (3)

mmerle
mmerle

Reputation: 595

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();

Upvotes: 1

Adriel Jr
Adriel Jr

Reputation: 2681

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);

Upvotes: 5

ajshort
ajshort

Reputation: 3764

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();
}

Upvotes: 8

Related Questions