After having showcased Qt GUI 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 more depth. Things that have been left out in the tutorial for pedagogical reasons will be covered here, as well as some interesting topics we haven't touched upon yet.
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 GUI 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 lets you view and edit already existing connections between components. It shows in a tabular display for each connection 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 and the slots of the receiving widget, as well as a list of already existing connections. Simply by clicking on a slot you can add a new connection between these two components. By marking a connection, the Disconnect button becomes available and lets you remove a connection again. From here, you even have access to the Slot tool for creating your own slots that we will cover in the next section.
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 between which there are already existing connections. You cannot add new component pairs here. This is exclusively done graphically in the form editor. It should be added that it is not possible to create a new connection with the keyboard only; 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 know already will open.
In case you hit F3 by accident, it is good to know how to leave this distressing mode where everything gets suddenly 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 even works if you already dragging, but have not yet released the mouse button, but depending on your mouse and keyboard, it might be difficult to operate the mouse and hit a keyboard button 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 then.
In simpler dialogs, just using 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 reach 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, this will of course only be 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 your are writing. These connections would then not be made within Qt GUI Designer, but rather in hand-written 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 only be called from within the same class or from within classes that inherit from it. A private method finally can only be called 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, 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? Well, as you have learned in the tutorial already, 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 any longer which would mean that it would be confined to one class. It would simply not be useful in the Qt GUI 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 that has 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 it is a slot). 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 fill the following into 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 GUI 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.
[1] | Please note that it is perfectly OK to make slots private in classes that are not created with the designer. This is a very common design pattern in hand-crafted forms, where private slots implement dialog-internal changes, like disabling or enabling some widgets when the state of another widget changes. |