如何使QLineEdit在Windows中不可编辑

编程入门 行业动态 更新时间:2024-10-22 23:08:31
本文介绍了如何使QLineEdit在Windows中不可编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用Qt 5.2,我想使 QLineEdit 不可编辑.问题是,它看起来不像它.使用 setReadOnly(true)时,它保持白色背景,看起来仍然可以编辑.

I'm using Qt 5.2 and I would like to make a QLineEdit not editable. The problem with this is, that it doesn't appear like it. When using setReadOnly(true) it stays with white background and looks like it is still editable.

如果我禁用它,则它将变成灰色,并且文本也将变为浅灰色.问题是,在禁用状态下,无法从中复制文本.

If I disable it, then it turns gray and the text also gets a lighter gray. The problem is, that one can not copy the text from it, in a disabled state.

因此,如何使 QLineEdit 正确地不可编辑并使它看起来也是如此.在Windows中,此类控件通常为灰色,但文本保持黑色.当然,我可以手动设置样式,但这意味着它是硬编码的,在其他平台上可能看起来是错误的.

So how can I make a QLineEdit properly non-editable and also make it look like it. In Windows such a control is usually gray, but the text stays black. Of course I could set the style manually, but this means that it is hard-coded and may look wrong on other platforms.

推荐答案

将行编辑为只读后,您可以将背景和文本颜色设置为您喜欢的任何颜色:

After making the line edit readonly, you can set the background and text colors to whatever you like :

ui->lineEdit->setReadOnly(true); QPalette *palette = new QPalette(); palette->setColor(QPalette::Base,Qt::gray); palette->setColor(QPalette::Text,Qt::darkGray); ui->lineEdit->setPalette(*palette);

更多推荐

如何使QLineEdit在Windows中不可编辑

本文发布于:2023-11-23 01:25:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1619612.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:编辑   QLineEdit   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!