source: trunk/doc/src/signalsandslots.qdoc@ 109

Last change on this file since 109 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 18.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the documentation of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \page signalsandslots.html
44 \title Signals and Slots
45 \ingroup architecture
46 \brief An overview of Qt's signals and slots inter-object
47 communication mechanism.
48
49 Signals and slots are used for communication between objects. The
50 signals and slots mechanism is a central feature of Qt and
51 probably the part that differs most from the features provided by
52 other frameworks.
53
54 \tableofcontents
55
56 \section1 Introduction
57
58 In GUI programming, when we change one widget, we often want
59 another widget to be notified. More generally, we want objects of
60 any kind to be able to communicate with one another. For example,
61 if a user clicks a \gui{Close} button, we probably want the
62 window's \l{QWidget::close()}{close()} function to be called.
63
64 Older toolkits achieve this kind of communication using
65 callbacks. A callback is a pointer to a function, so if you want
66 a processing function to notify you about some event you pass a
67 pointer to another function (the callback) to the processing
68 function. The processing function then calls the callback when
69 appropriate. Callbacks have two fundamental flaws: Firstly, they
70 are not type-safe. We can never be certain that the processing
71 function will call the callback with the correct arguments.
72 Secondly, the callback is strongly coupled to the processing
73 function since the processing function must know which callback
74 to call.
75
76 \section1 Signals and Slots
77
78 In Qt, we have an alternative to the callback technique: We use
79 signals and slots. A signal is emitted when a particular event
80 occurs. Qt's widgets have many predefined signals, but we can
81 always subclass widgets to add our own signals to them. A slot
82 is a function that is called in response to a particular signal.
83 Qt's widgets have many pre-defined slots, but it is common
84 practice to subclass widgets and add your own slots so that you
85 can handle the signals that you are interested in.
86
87 \img abstract-connections.png
88 \omit
89 \caption An abstract view of some signals and slots connections
90 \endomit
91
92 The signals and slots mechanism is type safe: The signature of a
93 signal must match the signature of the receiving slot. (In fact a
94 slot may have a shorter signature than the signal it receives
95 because it can ignore extra arguments.) Since the signatures are
96 compatible, the compiler can help us detect type mismatches.
97 Signals and slots are loosely coupled: A class which emits a
98 signal neither knows nor cares which slots receive the signal.
99 Qt's signals and slots mechanism ensures that if you connect a
100 signal to a slot, the slot will be called with the signal's
101 parameters at the right time. Signals and slots can take any
102 number of arguments of any type. They are completely type safe.
103
104 All classes that inherit from QObject or one of its subclasses
105 (e.g., QWidget) can contain signals and slots. Signals are emitted by
106 objects when they change their state in a way that may be interesting
107 to other objects. This is all the object does to communicate. It
108 does not know or care whether anything is receiving the signals it
109 emits. This is true information encapsulation, and ensures that the
110 object can be used as a software component.
111
112 Slots can be used for receiving signals, but they are also normal
113 member functions. Just as an object does not know if anything receives
114 its signals, a slot does not know if it has any signals connected to
115 it. This ensures that truly independent components can be created with
116 Qt.
117
118 You can connect as many signals as you want to a single slot, and a
119 signal can be connected to as many slots as you need. It is even
120 possible to connect a signal directly to another signal. (This will
121 emit the second signal immediately whenever the first is emitted.)
122
123 Together, signals and slots make up a powerful component programming
124 mechanism.
125
126 \section1 A Small Example
127
128 A minimal C++ class declaration might read:
129
130 \snippet doc/src/snippets/signalsandslots/signalsandslots.h 0
131
132 A small QObject-based class might read:
133
134 \snippet doc/src/snippets/signalsandslots/signalsandslots.h 1
135 \codeline
136 \snippet doc/src/snippets/signalsandslots/signalsandslots.h 2
137 \snippet doc/src/snippets/signalsandslots/signalsandslots.h 3
138
139 The QObject-based version has the same internal state, and provides
140 public methods to access the state, but in addition it has support
141 for component programming using signals and slots. This class can
142 tell the outside world that its state has changed by emitting a
143 signal, \c{valueChanged()}, and it has a slot which other objects
144 can send signals to.
145
146 All classes that contain signals or slots must mention
147 Q_OBJECT at the top of their declaration. They must also derive
148 (directly or indirectly) from QObject.
149
150 Slots are implemented by the application programmer.
151 Here is a possible implementation of the \c{Counter::setValue()}
152 slot:
153
154 \snippet doc/src/snippets/signalsandslots/signalsandslots.cpp 0
155
156 The \c{emit} line emits the signal \c valueChanged() from the
157 object, with the new value as argument.
158
159 In the following code snippet, we create two \c Counter objects
160 and connect the first object's \c valueChanged() signal to the
161 second object's \c setValue() slot using QObject::connect():
162
163 \snippet doc/src/snippets/signalsandslots/signalsandslots.cpp 1
164 \snippet doc/src/snippets/signalsandslots/signalsandslots.cpp 2
165 \codeline
166 \snippet doc/src/snippets/signalsandslots/signalsandslots.cpp 3
167 \snippet doc/src/snippets/signalsandslots/signalsandslots.cpp 4
168
169 Calling \c{a.setValue(12)} makes \c{a} emit a
170 \c{valueChanged(12)} signal, which \c{b} will receive in its
171 \c{setValue()} slot, i.e. \c{b.setValue(12)} is called. Then
172 \c{b} emits the same \c{valueChanged()} signal, but since no slot
173 has been connected to \c{b}'s \c{valueChanged()} signal, the
174 signal is ignored.
175
176 Note that the \c{setValue()} function sets the value and emits
177 the signal only if \c{value != m_value}. This prevents infinite
178 looping in the case of cyclic connections (e.g., if
179 \c{b.valueChanged()} were connected to \c{a.setValue()}).
180
181 A signal is emitted for every connection you make; if you
182 duplicate a connection, two signals will be emitted. You can
183 always break a connection using QObject::disconnect().
184
185 This example illustrates that objects can work together without needing to
186 know any information about each other. To enable this, the objects only
187 need to be connected together, and this can be achieved with some simple
188 QObject::connect() function calls, or with \c{uic}'s
189 \l{Using a Designer .ui File in Your Application#Automatic Connections}
190 {automatic connections} feature.
191
192 \section1 Building the Example
193
194 The C++ preprocessor changes or removes the \c{signals},
195 \c{slots}, and \c{emit} keywords so that the compiler is
196 presented with standard C++.
197
198 By running the \l moc on class definitions that contain signals
199 or slots, a C++ source file is produced which should be compiled
200 and linked with the other object files for the application. If
201 you use \l qmake, the makefile rules to automatically invoke \c
202 moc will be added to your project's makefile.
203
204 \section1 Signals
205
206 Signals are emitted by an object when its internal state has changed
207 in some way that might be interesting to the object's client or owner.
208 Only the class that defines a signal and its subclasses can emit the
209 signal.
210
211 When a signal is emitted, the slots connected to it are usually
212 executed immediately, just like a normal function call. When this
213 happens, the signals and slots mechanism is totally independent of
214 any GUI event loop. Execution of the code following the \c emit
215 statement will occur once all slots have returned. The situation is
216 slightly different when using \l{Qt::ConnectionType}{queued
217 connections}; in such a case, the code following the \c emit keyword
218 will continue immediately, and the slots will be executed later.
219
220 If several slots are connected to one signal, the slots will be
221 executed one after the other, in an arbitrary order, when the signal
222 is emitted.
223
224 Signals are automatically generated by the \l moc and must not be