【问题标题】:PyQt - Change the color of a QLineEdit if it has no textPyQt - 如果 QLineEdit 没有文本,则更改其颜色
【发布时间】:2018-02-15 08:08:23
【问题描述】:

是否可以仅使用其样式表来更改 QLineEdit 的背景颜色,具体取决于其中是否写入了文本?

当然,这可以使用 Python 代码来完成,但我想知道是否可以使用 css 属性来完成。

我的理想场景:

CSS

QLineEdit {
    background-color: white;
}

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

快速旁注 - 根据此 SO 帖子的答案:Changing the Color of QLineEdit's Placeholder Text,无法直接修改占位符,因此看起来 QLineEdit 可能没有此级别的功能。但我知道在 Qt 4.7 中对占位符进行了一些调整,所以现在可能是这样

也就是说,有人知道我正在寻找的东西是否可行?

【问题讨论】:

    标签: python pyqt


    【解决方案1】:

    试试这个(适用于 PyQt5,也应该适用于 PyQt4):

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-07
      • 1970-01-01
      • 2013-01-03
      • 1970-01-01
      • 1970-01-01
      • 2015-01-29
      • 1970-01-01
      • 2015-02-10
      相关资源
      最近更新 更多