PyQt offers several different ways of customizing widget appearance and behavior. The simplest and most frequently used approach is to set existing widget properties to the values we want. From Qt 4.2, the style sheet property is available, and this allows us to have a dramatic effect on the appearance of our widgets simply by entering plain text using the CSS syntax. One common and very easy use of style sheets is to set the background color of mandatory widgets.
Composite widgets allow us to lay out two or more widgets together and to then treat the resultant widget as a single widget. This can save time if the composite widget is used a lot, and also provides a way of having more than one widget in a main window-style application's central area. Some programmers make the constituent widgets private and forward their signals and slots, but in many cases the simplest approach is to leave the constituent widgets as public instance variables and to access and connect to them directly.
Subclassing existing widgets to adapt their appearance and especially their behavior is a lot easier than creating a QWidget subclass and doing everything ourselves. This approach works well with almost every PyQt widget, since most of them are designed to be subclassed. The only limitation of this approach is that it can be applied only to widgets that are similar enough to the widget we want, to make the adaptation feasible.
If we need to create a widget unlike any other, or if we want complete control over the appearance and behavior of our custom widget, we can subclass QWid-get. Our subclass must reimplement paintEvent(), sizeHint(), and minimumSize-Hint(), and will almost always reimplement keyPressEvent() and some of the mouse event handlers. Most of the built-in widgets are created in this way, with the rest being subclasses of other built-in widgets.
All the widgets we have customized or created in this chapter, and indeed throughout the book, are quite conventional in their appearance and behavior. PyQt does not enforce such conservatism, and we are free to create widgets with any appearance and any behaviors we can imagine.
Was this article helpful?
Download Tube Jacker And Discover Everything You Need To Know About Jacking Unlimited Traffic From The Video Giant. The drop dead easy way to create winning video campaigns that will FLOOD your website with unstoppable FREE traffic, all on complete and total autopilot. How to exploit a sneaky method of boosting exposure and getting your videos to the top of Google within 72 hours, guaranteed.