QMessageBox "show details"

When you open a QMessageBox with detailed typing, it has a show info button. I would like the data to be displayed by default, and not to the user, first click the "Show Details ..." button.

qt_doc_example

+5
source share
2 answers

As far as I can tell by a quick look at the source , there is no easy way to directly open the text of the part or really access to the button "Show details ...". The best way I could find was this:

  • Iterate through all the buttons in the message box.
  • ActionRole value with the ActionRole role, as this corresponds to the "Show Details ..." button.
  • Call the click method manually.

Example code for 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(); } 
+2
source

This function will expose details by default, and also resize the text box to a larger 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); 
0
source

Source: https://habr.com/ru/post/1245309/


All Articles