[英]PyQt5: Change layout for multiple objects at once
Okay, so I have a QMainWindow object that is my window and that contains several QLineEdit objects. 好的,所以我有一个QMainWindow对象,它是我的窗口,其中包含几个QLineEdit对象。 Depending on a condition, a single QLineEdit object can change it's color (color: red if error). 根据条件,单个QLineEdit对象可以更改其颜色(颜色:如果有错误,则为红色)。 I'm trying to find a way to reset the color of all QLineEdit objects back to black (default) when pressing a button. 我试图找到一种方法,当按下按钮时,将所有 QLineEdit对象的颜色重置为黑色(默认)。 Right now, I have them all in a single list and iterate through them, which is inefficient because I have ~60 QLineEdit objects. 现在,我将它们全部放在一个列表中并对其进行遍历,这效率不高,因为我有约60个QLineEdit对象。
I'm looking for a way to change the stylesheet globally for all QLineEdit objects within my window, at once. 我正在寻找一种可以一次全局更改窗口中所有QLineEdit对象的样式表的方法。
Ui_MainWindow is a class that is automatically generated by Qt Designer and imported. Ui_MainWindow是由Qt Designer自动生成并导入的类。 I make all the changes to my class MainWindow, instead of Ui_MainWindow, as Qt Desginer tells me to. 我对类MainWindow进行了所有更改,而不是Qt Desginer告诉我的Ui_MainWindow。
from PyQt5.QtWidgets import QMainWindow
from Windows.main_window import Ui_MainWindow
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self):
super(QMainWindow, self).__init__()
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Input validation
self.ui.lineEdit.textChanged.connect(lambda: self.validate(self.ui.lineEdit))
self.ui.lineEdit_2.textChanged.connect(lambda: self.validate(self.ui.lineEdit_2))
self.ui.lineEdit_3.textChanged.connect(lambda: self.validate(self.ui.lineEdit_3))
# Open file button
self.ui.pushButton_Open.clicked.connect(self.open_file_dialog)
self.show()
# If text meets a condition, paint it red
def validate(self, element):
if element.text() == 'foo':
element.setStyleSheet('color: rgb(255,0,0)')
def open_file_dialog(self):
self.reset_stylesheet()
...
def reset_stylesheet(self):
ui_elements = [self.ui.lineEdit, self.ui.lineEdit_2, self.ui.lineEdit_3]
for element in ui_elements:
element.setStyleSheet('color: rgb(0,0,0)')
You could try something like this but I'm not sure how much faster it will be. 您可以尝试类似的方法,但是我不确定它会快多少。
I changed the connect methods to line.textChanged.connect(self.validate)
though you could change them back use element.setProperty
in the validate
funciton 我将连接方法更改为line.textChanged.connect(self.validate)
尽管您可以在validate
使用element.setProperty
将其更改回
Add the top two lines to the __init__
method. 将前两行添加到__init__
方法中。
self.setStyleSheet('QLineEdit[validated=true]{color: rgb(255,0,0)}')
self.lineEdits = (x for x in self.centralWidget().children()
if isinstance(x, QLineEdit))
# If text meets a condition, paint it red
def validate(self, element):
if element == 'foo':
self.sender().setProperty('validated',True)
else:
self.sender().setProperty('validated',False)
self.sender().setStyle(self.style())
def invalidate(self, element):
element.setProperty('validated', False)
element.setStyle(element.style())
return element
def open_file_dialog(self):
self.reset_stylesheet()
def reset_stylesheet(self):
tuple(map(self.invalidate, self.lineEdits))
Or you could just add 或者您可以添加
self.setStyleSheet('QLineEdit{color:#FFF};')
in the reset_stylesheet()
function reset_stylesheet()
函数中的self.setStyleSheet('QLineEdit{color:#FFF};')
and 和
self.setStyleSheet('QLineEdit{color:#F00};')
in the validate section. 验证部分中的self.setStyleSheet('QLineEdit{color:#F00};')
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.