Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QMessageBox "show details"

Tags:

qt

qmessagebox

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

like image 523
UmNyobe Avatar asked Mar 18 '16 11:03

UmNyobe


People also ask

How do you show messages in Qt?

To use the property-based API, construct an instance of QMessageBox, set the desired properties, and call exec() to show the message. The simplest configuration is to set only the message text property. QMessageBox msgBox; msgBox. setText("The document has been modified."); msgBox.

How do I change the size of my QMessageBox?

You can edit the css of the label: msg. setStyleSheet("QLabel{min-width: 700px;}"); You can similarly edit the css of the buttons to add a margin or make them bigger.


3 Answers

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();
}
like image 60
ajshort Avatar answered Sep 27 '22 19:09

ajshort


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);
like image 39
Adriel Jr Avatar answered Sep 27 '22 18:09

Adriel Jr


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();
like image 37
mmerle Avatar answered Sep 27 '22 18:09

mmerle