Chapter 5. Useful Techniques

Table of Contents
Working With Signals And Slots
Changing The Tab Order
All About Layout Management
Integrating Qt Designer Files Into Your Project
Accessing The Help System
Custom Widgets

Having showcased Qt Designer in the previous tutorial-style chapters, we will go a bit deeper in the following sections and explain a number of useful techniques in depth. Things that were left out of the tutorial for pedagogical reasons will be covered here, as well as some interesting topics we haven't touched upon yet.

Working With Signals And Slots

As you know, the concept of signals and slots is one of the central ideas in Qt. It is therefore not very astonishing that Qt Designer provides two means to facilitate the use of signals and slots in Qt dialogs: the slots tool and the connection tool.

The Connection Tool

The connection tool lets you view and edit already existing connections between components. In a tabular display for each connection, it shows the sender by name, the emitted signal, the receiver by name, and the slot to which the signal is connected (see Figure 5-1). If you want to make an additional connection between a sending and a receiving object, you can click on the Edit button. A new window pops up that shows the signals of the sending widget GUI element and the slots of the receiving widget, as well as a list of already existing connections. By simply clicking on a slot, you can add a new connection between these two components. When you mark a connection, the Disconnect button becomes available and lets you remove a connection again. From here, you even have access to the slots tool for creating your own slots, which we will cover in the next section.

Figure 5-1. The connection tool

Note that the connection tool does not distinguish between predefined Qt slots and slots that you have defined yourself. By simply creating your own slots, you can easily augment the connection possibilities.

As useful as the connection tool is, it only lets you delete connections or add connections between those components that already have existing connections. You cannot add new component pairs here. This is exclusively done graphically in the form editor. Also, it is not possible to create a new connection with only the keyboard ; you always need to use the mouse technique described here.

You have already seen how to connect two widgets in the form editor: Simply hit the F3 key or click on the connection icon. The mouse cursor will change into a cross. Click on the widget that should be the sender in your new connection, and see how a pink rectangle is drawn around it. Now move the mouse cursor across your form and watch how a second pink rectangle surrounds your current target object; i.e., the object that is on the receiving side of the connection. Once you release the mouse button, the connection tool window that you already know will open.

In case you hit F3 by accident, it is good to know how to leave this distressing mode where everything suddenly gets pink. If you have not started dragging the mouse—i.e., you have not even clicked on the sending component—you can simply hit the Esc key to get out of this mode. This works even if you are already dragging the mouse button but have not yet released it. However, depending on your mouse and keyboard, it might be difficult to operate the mouse and hit a key at the same time. In this case, either click on the pointer tool icon or just release the mouse anywhere and click Cancel in the dialog that pops up; no connection whatsoever will be made.

The Slots Tool

In simpler dialogs, using just the predefined slots is probably enough. But in more sophisticated dialogs, you will most likely want to define your own slots. This is of course possible; you do it with the slots tool. You access the slots tool by selecting Edit/Slots from the menu. A dialog window will pop up that shows all slots of the form you are currently editing. If you have not added any of your own yet, you will see only Qt's predefined slots. Each slot is listed with its name and parameters, its access specifier, and whether it is “in use,” meaning whether it has a signal connected to it. Note that it is not necessary for each slot to be in use: A slot could be defined for outside use, for connections being made from other forms or other parts of the application you are writing. These connections would then be made not within Qt Designer but in handwritten code.

The access specifier might need some explanation. As you probably know, a public C++ method can be called from within any other class, but a protected method can be called only from within the same class or from within classes that inherit from it. A private method can be called only from within the same class. The same applies to slots, because at the end of the day, slots are just C++ methods.

When should a slot be public, and when should it be protected? Here, the same criteria apply as for ordinary methods that are not slots: If the slot will be connected to from outside of the class, it must be public; otherwise, it should be protected, following the rule to expose as little of the functionality of a class as possible to the outside.

If you look at the Slot Properties group box, where you can change the properties of the currently marked slot, and more specifically at the combo box, where you can select the access specifier, you might notice that it is not possible to make a slot private. Why is that? As you have already learned in the tutorial, in order to implement your own functionality, you need to subclass from the created form class and implement the functionality (whether it is in a slot or elsewhere) there. But since we now have a hierarchy of classes, the slot cannot be made private, because that would make it impossible to call it from another class. It would simply not be useful in the Qt Designer context any longer. [1]

OK, so now you know how you can change the name and access specifier of a slot: Click on the slot in the slot list, and change the name and parameters and/or the access specifier in the Slot Properties group box. It is probably obvious that you can remove a slot by marking it and clicking the Delete Slot button. Thus, the only thing left to explain is how to create new slots. To do this, click on the New Slot button. A new slot will be added to the slot list with the name new_slot(), no parameters, and public access. It is marked as the current slot, so you can edit its properties right away. Think whether you want to connect to this slot from the outside (in which case it should have public access) or not (in which case it should have protected access). Change the name to something more descriptive (many developers like to start the names of their slots with slot to make it more obvious that they are slots). Finally, if your slot should have parameters, add these between the parentheses after the slot name. For example, if the slot should have two integer parameters and be called slotMovePosition(), you would provide the following in the edit field:

slotMovePosition(int,int)

Note that you do not have to (and should not) specify the return type; slots always have void as their return type, which is added by Qt Designer automatically.

All changes that you make are directly taken over to the slot list, so you do not need to “save” your changes anywhere. Just click the OK button when you are done. On the other hand, closing the dialog with the Cancel button or the Esc key will remove your changes.

Notes

[1] It is perfectly OK to make slots private in classes that are not created with Qt Designer. This is a very common design pattern in handcrafted forms, where private slots implement dialog-internal changes, like disabling or enabling some widgets when the state of another widget changes.