Grey-out QLineEdit as per disabled state automatically, combined with non-conditional style sheet directive?

26 Views Asked by At

By default, a QLineEdit is greyed-out when disabled.

If you set a stylesheet with a simple directive, e.g. setting a border, this cancels that default behaviour.

Fortunately it is possible to mimic the default behaviour, like so:

qle.setStyleSheet('QLineEdit[readOnly=\"true\"] {color: #808080; background-color: #F0F0F0;}')

But how might I combine this conditional greying-out with setting a border (unconditionally)?

Here is an MRE:

import sys
from PyQt5 import QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QLE stylesheet question")
        self.setMinimumSize(400, 30)
        layout = QtWidgets.QVBoxLayout()
        qle = QtWidgets.QLineEdit()
        qle.setObjectName('qle')
        # qle.setStyleSheet('border:4px solid yellow;')
        # this sets up the style sheet to reproduce the default PyQt behaviour for a QLE (greyed-out if disabled, otherwise not)
        # qle.setStyleSheet('QLineEdit[readOnly=\"true\"] {color: #808080; background-color: #F0F0F0;}')
        
        # doesn't work: when disabled is not greyed-out!
        qle.setStyleSheet('#qle {border:4px solid yellow;} #qle[readOnly=\"true\"] {color: #808080; background-color: #F0F0F0;}')
        
        # qle.setEnabled(False) # uncomment to see "disabled" appearance
        layout.addWidget(qle)
        self.setLayout(layout)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
0

There are 0 best solutions below