默认情况下,禁用时,
QLineEdit
会显示为灰色。
如果您使用简单的指令设置样式表,例如设置边框,这会取消默认行为。
幸运的是,可以模仿默认行为,如下所示:
qle.setStyleSheet('QLineEdit[readOnly=\"true\"] {color: #808080; background-color: #F0F0F0;}')
但是我如何将这种有条件的灰化与设置边框(无条件)结合起来?
这是 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_())
QLineEdit::disabled{
background-color:#eeeeee;
}