根据禁用状态自动灰显 QLineEdit,并结合无条件样式表指令?

问题描述 投票:0回答:1

默认情况下,禁用时,

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_())
    
python pyqt qtstylesheets
1个回答
0
投票
将这个样式表用于QLineEdit就达到了目的:

QLineEdit::disabled{ background-color:#eeeeee; }
    
© www.soinside.com 2019 - 2024. All rights reserved.