简体   繁体   English

PyQt-如果没有文本,请更改QLineEdit的颜色

[英]PyQt - Change the color of a QLineEdit if it has no text

Is it possible to change a QLineEdit background color depending on if it has text written inside it, using just only its stylesheet? 仅使用样式表,是否可以根据QLineEdit背景颜色来更改QLineEdit背景颜色?

This could be done using Python code, of course, but I was wondering if it could be done using a css property. 当然,这可以使用Python代码完成,但是我想知道是否可以使用css属性来完成。

My ideal scenario: 我理想的情况是:

CSS 的CSS

QLineEdit {
    background-color: white;
}

QLineEdit:no-text-inside-it {
    background-color: red;
}

Quick side note - According to the answer to this SO post: Changing the Color of QLineEdit's Placeholder Text , the placeholder can't be modified directly so it looks like QLineEdit may not have this level of functionality. 快速旁注-根据此SO帖子的答案: 更改QLineEdit的占位符文本的颜色,无法直接修改占位 ,因此看起来QLineEdit可能没有此功能。 But I know that some adjustments to placeholders were made in Qt 4.7, so may be that's a way, now 但是我知道Qt 4.7对占位符进行了一些调整,所以现在可能是这样

That said, anyone know if what I'm looking for is possible? 就是说,有人知道我在寻找什么可能吗?

Try this (works in PyQt5, should work in PyQt4 too): 试试这个(在PyQt5中可以使用,在PyQt4中也可以使用):

QLineEdit
{
    background-color: black;
}

QLineEdit[text=""]
{
    background-color: red;
}

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

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