Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QMessageBox you can select text from

Tags:

qt

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.

like image 381
Petr Avatar asked Sep 15 '15 15:09

Petr


People also ask

How do I add a button to a qmessagebox?

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

How does qmessagebox find the ESCAPE button in a text message?

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.

How do I get the text of a button in PyQt5 qmessagebox?

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.

What is a message box in Qt?

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.


2 Answers

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();
like image 197
Alexander Sorokin Avatar answered Jan 04 '23 10:01

Alexander Sorokin


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.

like image 24
Marek R Avatar answered Jan 04 '23 10:01

Marek R