Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Qt Stylesheet for all Instances of a Widget

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 :)

like image 777
HatHat556 Avatar asked Nov 04 '25 16:11

HatHat556


1 Answers

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_())
like image 155
jfaccioni Avatar answered Nov 06 '25 08:11

jfaccioni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!