User Tools

Site Tools


qt:signals_and_slots

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
qt:signals_and_slots [2010/12/09 14:45] – [Writing your own signals] mithatqt:signals_and_slots [2011/04/01 11:10] (current) mithat
Line 1: Line 1:
 ====== Signals and slots ====== ====== Signals and slots ======
  
-Communication between objects in Qt is typically handled by a signal and slots system. Signals are emitted by objects in response to user interaction or other stimuli, and these signals are received by slots in other objects. Connections are made between signals and slots by the programmer using some simple non-C++ code foo. The behavior associated with a received signal is determined programatically. Lots of Qt classes have predefined signals and slots. You can implement new signals and slots in classes you derive yourself.+Communication between objects in Qt is typically handled by a signal and slots system. Signals are emitted by objects in response to user interaction or other stimuli, and these signals are received by slots in other objects. Connections are made between signals and slots by the programmer using some Qt macro foo. The behavior associated with a received signal is determined programatically. Lots of Qt classes have predefined signals and slots. You can implement new signals and slots in classes you derive yourself.
  
 ===== Making connections ===== ===== Making connections =====
 A connection between a signal and a slot is made as follows: A connection between a signal and a slot is made as follows:
 <code cpp-qt> <code cpp-qt>
-QObject::connect({pointer to emiter}, SIGNAL({signal-name}({parameter-type(s)})), +QObject::connect(<pointer-to-emiter>, SIGNAL(<signal-name>(<parameter-type(s)>)), 
-                 {pointer to receiver}, SLOT({slot-name}({parameter-type(s)})));+                 <pointer-to-receiver>, SLOT(<slot-name>(<parameter-type(s)>)));
 </code> </code>
  
-The emiter emits the signal ''{signal-name}'' with a list of values whose types are identified with ''{parameter-types(s)}''. The receiver receives this signal into its ''{slot-name}'' slot along with the values emitted with the signal. The slot then takes the appropriate action depending on the value of the passed parameters.+The emiter emits the signal ''<signal-name>'' with a list of values whose types are identified with ''<parameter-types(s)>''. The receiver receives this signal into its ''<slot-name>'' slot along with the values emitted with the signal. The slot then takes the appropriate action depending on the value of the passed parameters. 
  
-For example:((From Molkentin, p. 37))+Example:((From Molkentin, p. 37))
 <code cpp-qt> <code cpp-qt>
 QSpinBox* spinBox = new QSpinBox; QSpinBox* spinBox = new QSpinBox;
Line 24: Line 24:
  
 The ''valueChanged'' signal is defined for QSpinBox and the ''setNum'' slot is defined for QLabel. ''valueChanged'' emits an ''int'', and ''setNum'' expects to receive an ''int''. Whenever ''spinBox'''s value changes (e.g., in response to user interaction) it will emit the ''valueChanged'' signal along with its updated value (as an ''int''). ''label'''s ''setNum'' receives the signal, which causes ''label'' update itself and show the (''int'') value received. The ''valueChanged'' signal is defined for QSpinBox and the ''setNum'' slot is defined for QLabel. ''valueChanged'' emits an ''int'', and ''setNum'' expects to receive an ''int''. Whenever ''spinBox'''s value changes (e.g., in response to user interaction) it will emit the ''valueChanged'' signal along with its updated value (as an ''int''). ''label'''s ''setNum'' receives the signal, which causes ''label'' update itself and show the (''int'') value received.
 +
 +The number of parameters emited by a signal is allowed to be greater than the number of parameters received by a slot; extra parameters are ignored.
 +
 +Errors in connecting signals and slots are not reported in compiling or linking. However, they will produce a runtime warning: \\
 +\\
 +''Object::connect: Incompatible sender/receiver arguments YourClass::aSignal(char,double) --> AnotherClass::aSlot(double)''
  
 ===== Multiple connections ===== ===== Multiple connections =====
-Signals may connect to multiple slots. (FIXME Is the reverse true?)+Signals may connect to multiple slots. Multiple signals may also connect to a single slot.
  
-If a signal connects to multiple slots, the execution order of the slots is undefined.+In the case of multiple connections, the execution order of the slots or signals is undefined.
  
 ===== Writing your own slots ===== ===== Writing your own slots =====
qt/signals_and_slots.1291905908.txt.gz · Last modified: 2010/12/09 14:45 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki