简体   繁体   English

更改QLineEdit占位符文本的颜色

[英]Changing the Color of QLineEdit's Placeholder Text

I am writing an application in Qt 4.8 and taking advantage of styling through CSS. 我在Qt 4.8中编写了一个应用程序,并利用了CSS样式。 When styling QLineEdit there are properties such as selection-background-color , is there a property that will allow me to change the color of placeholder text? 设置QLineEdit样式时,有一些属性,例如selection-background-color ,是否有一个属性可以让我更改占位符文本的颜色?

If not, are there any other methods of changing it? 如果没有,是否还有其他更改方法?

The color of the placeholder is the color of the text with alpha set to 50%. 占位符的颜色是alpha设置为50%的文本的颜色。 At least in 4.7 this was hardcoded and could thus not be changed with style. 至少在4.7中,它是硬编码的,因此不能随样式更改。

As the decision is implemented in QLineEdit::paintEvent you could override paintEvent in a derived class and change the color to whatever suits you fine. 由于此决定是在QLineEdit :: paintEvent中实现的,因此您可以在派生类中重写paintEvent并将颜色更改为适合您的颜色。

But why would you want a different color? 但是,为什么要换一种颜色呢? If you change it to red or something like that, you might irritate a user who cannot distinguish a placeholder from "real" text anymore. 如果将其更改为红色或类似的名称,则可能会激怒不能再将占位符与“真实”文本区分开的用户。

也许可以将调色板更改连接到信号textChanged():当行编辑为空时,更改文本颜色。

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

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