Changing GUI Appearances with Qt Style Sheets

Learn how we style our GUIs using style sheets provided by PyQt.

Up to this point, all your GUI designs have been primarily functional and less ornamented. It's equally crucial to design a layout to arrange widgets in a logical order as it changes how each widget looks and feels individually. Users can browse a user interface more quickly by picking the appropriate styles, colors, and fonts.

Using Qt Style Sheets

The QStyle class controls our applications' appearance when we use PyQt.

Several QStyle subclasses replicate the appearance of the system that the program is running on. In doing so, our GUI mimics the formation of a native macOS, Linux, or Windows application. QStyle classes can be modified, new classes can be created, or Qt Style Sheets can be used to create custom styles.

This chapter will examine the use of style sheets to develop unique styles. Customizing the appearance of widgets is possible with the help of Qt Style Sheets. HTML Cascading Style Sheets (CSS) was a model for the syntax used in Qt Style Sheets. Style sheets let you change how various widget characteristics, pseudostates, and subcontrols appear. You can change the background color, font size, color, border type, width, or style and add padding to widgets, among other things.

When a widget enters a specific state, such as going from active to disabled, or when the mouse hovers over it, pseudostates are used to characterize it. Subcontrols provide you access to a widget's child elements so you may modify their positioning, appearance, and other attributes. For instance, you could alter the color of a QCheckBox to indicate whether the button is checked or not.

Customizations can be made to the QApplication object or specific widgets using setStyleSheet().

Customizing individual widget properties

Let's begin by learning how to modify widgets. The backdrop color is changed to blue using the following code:

Get hands-on with 1200+ tech skills courses.