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.
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.
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.
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:
ActionRole
, as this corresponds to the "Show Details..." button.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();
}
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);
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With