| 1 |
Mini-tutorial on slots and signals |
| 2 |
********************************** |
| 3 |
|
| 4 |
This mini-tutorial from Mark Hammond explains how to use QtDesigner |
| 5 |
for editing signals-slots connections. |
| 6 |
|
| 7 |
On Figure 1 we have a screen-shot of one of our dialogs in QtDesigner |
| 8 |
in "Edit Widgets" (ie, normal) mode, with a signal/slot editor to the right. |
| 9 |
This little editor is generally in the tabbed window to the right |
| 10 |
of the screen, right near the Property Editor. |
| 11 |
Select "Tools->Signal/Slot Editor" to ensure it is visible. |
| 12 |
|
| 13 |
.. figure:: slot-signal-fig1.png |
| 14 |
|
| 15 |
Figure 1 |
| 16 |
|
| 17 |
|
| 18 |
Let's go through this list: |
| 19 |
|
| 20 |
* The first 2 elements in the list have a "Sender" as our 2 QLabel objects |
| 21 |
with the hyperlinks, and these objects are sending a linkActivated signal |
| 22 |
(which is builtin to QLabel objects). The receiver of the signal |
| 23 |
is the form itself (InitForm), and it receives the signal into a slot |
| 24 |
also called 'linkActivated' - linkActivated is *not* builtin to dialogs, |
| 25 |
but I've previously told QtDesigner that this form has such a slot - |
| 26 |
it could have used any name. As a result, we have linkActivated signals |
| 27 |
being 'forwarded' to our dialog. |
| 28 |
|
| 29 |
* The next 2 items in the list are showing the form itself as the sender |
| 30 |
of a signal called 'subprocessStarted(bool)' and the receiver of the events |
| 31 |
are groupboxes - but the slot is 'setDisabled'. Thus, whenever the form |
| 32 |
sends a signal of 'subprocessStarted(True)', the groupbox 'setDisabled(True)' |
| 33 |
will be called (and vice-versa). I've previously told QtDesigner |
| 34 |
that our form may send such a signal, and subprocess.py is where the actual |
| 35 |
emit() of the signal happens. As a result, we have the 2 groupboxes |
| 36 |
being automatically disabled when the subprocess starts and renabled |
| 37 |
if things fail. |
| 38 |
|
| 39 |
* The next item in the list if a signal from the 'but_init' widget, |
| 40 |
which is the 'Create a new standalone tree' radio button. |
| 41 |
The toggled(bool) signal is attached to the checkbox directly under it |
| 42 |
via the setEnabled(bool) slot. Thus, when the radio button is selected, |
| 43 |
it will send a toggled(True) signal, which will end up calling |
| 44 |
the checkbox's setEnabled(True) function. Thus, that checkbox depends on |
| 45 |
the state of the radio above it. |
| 46 |
|
| 47 |
* The final item in the list is the second radio button connected to |
| 48 |
the second checkbox, as described above. |
| 49 |
|
| 50 |
Now - we will switch QtDesigner into "Edit Signals/Slots" mode - |
| 51 |
select that mode from the Edit menu or press F4. |
| 52 |
Suddenly we will see lots of colors, see figure 2 below. |
| 53 |
|
| 54 |
.. figure:: slot-signal-fig2.png |
| 55 |
|
| 56 |
Figure 2 |
| 57 |
|
| 58 |
|
| 59 |
Note how Qt is trying to show all these signal slot relationships. |
| 60 |
Note that when a slot/signal is selected on the right, the connection is shown |
| 61 |
in pink on the form itself. It looks noisy, but it starts to make sense |
| 62 |
after a short time. |
| 63 |
|
| 64 |
By way of example, let's say I wanted to add a new label to the form |
| 65 |
with a new help link. See below figure 3 - I've added a new 'TextLabel' |
| 66 |
at the bottom of the form and selected it. |
| 67 |
|
| 68 |
.. figure:: slot-signal-fig3.png |
| 69 |
|
| 70 |
Figure 3 |
| 71 |
|
| 72 |
|
| 73 |
Now, I drag a line from that new widget to the dialog itself. |
| 74 |
A Qt dialog pops up asking me what connection I want to make between the objects. |
| 75 |
In this case, I want to connect the linkActivated signal from the label |
| 76 |
with the linkActivated slot on our dialog - see figure 4: |
| 77 |
|
| 78 |
.. figure:: slot-signal-fig4.png |
| 79 |
|
| 80 |
Figure 4 |
| 81 |
|
| 82 |
|
| 83 |
If you select OK, the Signal/Slot editor panel we started with |
| 84 |
will now show this new connection. |