简体   繁体   English

使QLabel文本可选?

[英]Make QLabel text selectable?

I have a QLabel in my application that displays error messages to the user. 我的应用程序中有一个QLabel ,它向用户显示错误消息。 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? 如何用鼠标选择QLabel的文本?

Code

The text of a QLabel can be made selectable by mouse like so: 可以通过鼠标选择QLabel的文本,如下所示:

label->setTextInteractionFlags(Qt::TextSelectableByMouse);

This is found in the QLabel documentation . 这可以在QLabel文档中找到。

You can use that same function to make links selectable by keyboard, highlight URL links, and make the text editable. 您可以使用相同的功能通过键盘选择链接,突出显示URL链接,并使文本可编辑。 See Qt::TextInteractionFlag . 参见Qt :: TextInteractionFlag

Designer 设计师

Search for textInteractionFlags under the QLabel menu and set the flag TextSelectableByMouse . QLabel菜单下搜索textInteractionFlags并设置标志TextSelectableByMouse

Here is another method, for reference... You could create a QLineEdit subclass instead, tweaked to look and act like a QLabel, in the constructor: 这是另一种方法,仅供参考...您可以改为在构造函数中创建QLineEdit子类,并对其进行调整以使其外观和行为类似QLabel:

 setReadOnly(true);
 setFrame(false);
 QPalette palette = this->palette();
 palette.setColor(QPalette::Base, palette.color(QPalette::Background));
 setPalette(palette);

I think the accepted answer is simpler and preferable to this though. 我认为公认的答案更简单,但更可取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM