Is there any way to display a QMessageBox
that would have selectable text, so that user would be able to use their mouse or keyboard to select and copy its contents somehow? I know I can create labels that do it, but not sure about the Message Boxes. Standard message box in MS Windows definitely doesn't seem to allow that.
Advanced Usage. If the standard buttons are not flexible enough for your message box, you can use the addButton() overload that takes a text and a ButtonRole to add custom buttons. The ButtonRole is used by QMessageBox to determine the ordering of the buttons on screen (which varies according to the platform).
If an escape button is not specified, QMessageBox tries to find one using these rules: If there is only one button, it is the button activated when Esc is pressed. If there is a Cancel button, it is the button activated when Esc is pressed.
When the messagebox button is clicked it automatically passes the button that was clicked to the function declared in buttonClicked.connect (). Calling the text () function on this button will return it’s text value. Head over to our main PyQt5 section to learn more about the other great widgets! This marks the end of the PyQt5 QMessageBox article.
The message box is an application modal dialog box. On macOS, if parent is not nullptr and you want your message box to appear as a Qt::Sheet of that parent, set the message box's window modality to Qt::WindowModal (default). Otherwise, the message box will be a standard dialog.
You need to enable the TextSelectableByMouse
interaction flag:
QMessageBox mb(QMessageBox::NoIcon, "New message",
"A lot of text", QMessageBox::Ok, this);
mb.setTextInteractionFlags(Qt::TextSelectableByMouse);
int dialogResult = mb.exec();
I prefer to solve this using style sheet.
Run this once and this will impact all message boxes created anywhere in application:
qApp->setStyleSheet("QMessageBox { messagebox-text-interaction-flags: 5; }");
I've test this with Qt 5.5 for OS X, and it works.
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