Okay, so I have a QMainWindow object that is my window and that contains several QLineEdit objects. Depending on a condition, a single QLineEdit object can change it's color (color: red if error). I'm trying to find a way to reset the color of all QLineEdit objects back to black (default) when pressing a button. Right now, I have them all in a single list and iterate through them, which is inefficient because I have ~60 QLineEdit objects.
I'm looking for a way to change the stylesheet globally for all QLineEdit objects within my window, at once.
Ui_MainWindow is a class that is automatically generated by Qt Designer and imported. I make all the changes to my class MainWindow, instead of Ui_MainWindow, as Qt Desginer tells me to.
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
Add the top two lines to the __init__
method.
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
and
self.setStyleSheet('QLineEdit{color:#F00};')
in the validate section.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.