Qt Designer provides a quick and easy way to create user interfaces. Using a visual design tool makes it much easier to see whether a design "works". Another benefit of Qt Designer is that if we change a design, providing we have not added, removed, or renamed any widgets we refer to in code, our code will not need to be changed at all. And even if we do add, rename, or remove widgets, the changes to our code may be quite small, since Qt Designer handles all the widget creation and laying out for us.

The fundamental principles of using Qt Designer are always the same: We drag widgets onto a form, containers (such as frames, group boxes, and tab widgets) first, then ordinary widgets, and we set their properties. Then we add spacers to occupy gaps. Next we select particular widgets, spacers, and layouts, and apply layouts to them, repeating this process until everything is laid out. Then we lay out the form itself. At the end we set buddies, the tab order, and the signal-slot connections.

Implementing dialogs with user interfaces that have been created by Qt Designer is similar to implementing them by hand. The biggest difference is in the initializer, where we simply call setupUi() to create and lay out the widgets, and to create the signal-slot connections. The methods we implement can be done just as we have done them before (and their code will be no different), but usually we use the onwidgetNamesignalName naming convention, along with the @pyqtSignature decorator to take advantage of setupUi()'s ability to automatically create connections.

A use case that we have not covered is to use the "Widget" template to create composite widgets (widgets made up of two or more other widgets laid out together). In some cases these widget designs can be used for entire forms, and in other cases they can be used as components of forms—for example, to provide the page of a tab widget or of a widget stack. Or two or more composite widgets could be laid out together in a form to create a more complex form. This use is possible by using Qt Designer and generating the Python modules in the normal way. Then we can import the generated modules, and in our form class, we call each custom widget's setupUi() method to create the user interface.

The questions about how smart a dialog is, what modality it should have, and how it validates are no different for dialogs created with Qt Designer than for those created by hand. The only exception that we can set widget properties in Qt Designer—for example, we could set a spinbox's range and initial value. We can, of course, do the same thing in code, but for widgets that need only simple validation, doing it all in Qt Designer is usually more convenient.

We must use pyuic4 to generate Python modules from Qt Designer .ui files, either by running pyuic4 directly or by using or Make PyQt, both of which also generate Python modules for resource files if .qrc files are present.

If we are not using testing tools, adding testing code that is executed only if the form is run stand-alone does not affect the performance of our dialogs, and can be very convenient both during development and when maintaining a dialog. If complex objects that the dialog depends on are not available, we can often create a "fake" class that provides the same methods as the complex object, and pass an instance of the fake class for testing purposes.

All PyQt programs can be written entirely by hand; there is never any need to use Qt Designer. However, designing dialogs with a visual design tool can be very helpful, since the results can be seen immediately, and changes to designs can be made quickly and easily. Another benefit of using Qt Designer is that a lot of fairly repetitive code for creating, laying out, and connecting widgets can be automatically generated rather than written by hand. Qt Designer was used to create a dialog in both this chapter, and the preceding one. We will see many more examples of dialogs created with Qt Designer in the following chapters.

Was this article helpful?

0 0
Tube Traffic Ninja

Tube Traffic Ninja

Discover How You Can Quickly And Easily Dominate Google and YouTube... With Simple Cash Generating Videos. Did you know that YouTube is the second largest search website on the entire Internet? YouTube gets more daily searches than Bing and Yahoo. In fact, there is only one search engine that gets more action.

Get My Free Ebook

Post a comment