Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make QLabel text selectable?

Tags:

c++

qt

I have a QLabel in my application that displays error messages to the user. I would like to make the text of the label selectable so users can copy and paste the error message if needed.

However, when I use the mouse to click and drag over the text, nothing happens - the text is not selected.

How can I make the text within a QLabel selectable by the mouse?

like image 817
Cory Klein Avatar asked Jul 30 '13 21:07

Cory Klein


People also ask

Is QLabel a Qwidget?

The properties and functions QLabel inherits from QFrame can also be used to specify the widget frame to be used for any given label. A QLabel is often used as a label for an interactive widget.

What is QLabel?

QLabel is used for displaying text or an image. No user interaction functionality is provided. The visual appearance of the label can be configured in various ways, and it can be used for specifying a focus mnemonic key for another widget.


1 Answers

Code

The text of a QLabel can be made selectable by mouse like so:

label->setTextInteractionFlags(Qt::TextSelectableByMouse); 

This is found in the QLabel documentation.

You can use that same function to make links selectable by keyboard, highlight URL links, and make the text editable. See Qt::TextInteractionFlag.

Designer

Search for textInteractionFlags under the QLabel menu and set the flag TextSelectableByMouse.

like image 142
Cory Klein Avatar answered Oct 07 '22 18:10

Cory Klein