I am creating qpushbuttons with a certain style and then updating the style sheet to color them later on. However when I do this it overwrites the original style. Is there a way I can update or append to the object's stylesheet without losing it or having to redefine everything again?
import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout,QPushButton, QMenu, QApplication, QWidget, QInputDialog, QLineEdit
from PyQt5.QtCore import QSize, QCoreApplication, Qt, QLine, QPoint
from PyQt5.QtGui import QPainter, QPen, QFont,QPainter, QPainterPath, QPixmap
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setMinimumSize(QSize(400, 400))
color = QPushButton('color', self)
color.clicked.connect(self.color)
color.resize(100,115)
color.move(0, 100)
buttons= ['button1','button2','button3']
self.pybutton = {}
x=0
t = 0
for i in buttons:
t = t + 100
x+=1
self.pybutton[str(x)] = QPushButton(i, self)
self.pybutton[str(x)].setObjectName('btn' + str(x))
self.pybutton[str(x)].resize(100,100)
self.pybutton[str(x)].move(400-int(t),100)
self.pybutton[str(x)].setStyleSheet('QPushButton {max-width: 75px;text-align:center;padding-left: 20px; max-height: 60px; font-size: 20px;}')
self.statusBar()
def status(self):
sender = self.sender()
print('PyQt5 button click')
self.statusBar().showMessage(sender.text() + ' was pressed')
def color(self):
for i in self.pybutton:
self.pybutton[str(i)].objectName()
if self.pybutton[str(i)].objectName() == 'btn1':
self.pybutton[str(i)].setStyleSheet("background-color: green")
else:
self.pybutton[str(i)].setStyleSheet("background-color: red")
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit( app.exec_() )