4

Using PyQt, is there any way to change the stylesheet for every instance of a widget without having to manually change each widget's stylesheet.

Say, for instance, I wanted every pushbutton in my application to have red text. How would I do this without having to run button.setStyleSheet() for each button? Could there possibly be a way to change PyQt's stock button class?

Any help would be appreciated :)

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
HatHat556
  • 91
  • 1
  • 8

1 Answers1

6

Set the style sheet of your QApplication instance to change the style of specific widgets globally, e.g.:

GLOBAL_STYLE = """QPushButton {
    color: red; 
}"""

class MyApp(QMainWindow): 
    # your ui code here


if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setStyleSheet(GLOBAL_STYLE)
    main = MyApp()
    main.show()
    sys.exit(app.exec_())
jfaccioni
  • 7,099
  • 1
  • 9
  • 25
  • This is a good answer. You can also set the style sheet for QMainWindow. The advantage to this is that if you have a complicated window layout, you can set the style sheet in Qt Designer and visualize it better. – bfris Jul 16 '20 at 05:08