Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

How to Use Signals and Slots: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(ToC)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Developing with Qt::General]]
[[Category:HowTo]]
[[Category:HowTo]]
[[Category:Snippets]]
[[Category:Tutorial]]
[[Category:Learning]]
[toc align_right="yes" depth="3"]
'''English''' [[How_to_Use_Signals_and_Slots_SimplifiedChinese|简体中文]]
= How to Use Signals and Slots? =
== Introduction ==
== Introduction ==
Remember old X-Windows ''call-back'' system? Generally it isn't type safe and flexible. There are many problems with them. Qt offers a new event handling system: ''signal-slot'' connections. Imagine an alarm clock. When alarm is ringing, a signal is being sent (''emit''). And you're handling it in a slot.


Remember old X-Window ''call-back'' system? Generally it isn't type safe and flexible. There are many problems with them. Qt offer new event-handling system - ''signal-slot'' connections. Imagine alarm clock. When alarm is ringing, signal is sending (''emitting''). And you're handling it as a slot.
* Every QObject class may have as many signals and slots as you want
 
* You can emit signals only from within that class, where the signal is located
* Every QObject class may have as many signals of slots as you want.
* You can emit signal only from that class, where signal is.
* You can ''connect'' signal with another signal (make chains of signals);
* You can ''connect'' signal with another signal (make chains of signals);
* Every signal and slot can have unlimited count of connections with other.
* Every signal and slot can have unlimited count of connections with other.
* '''ATTENTION!''' You can't set default value in slot attributes. ''e.g.'' <code>void mySlot(int i = 0);</code>
* '''ATTENTION!''' You can't set default value in slot attributes e.g. <tt>void mySlot(int i = 0);</tt>


== Connection ==
== Connection ==


You can connect signal with this template:
You can connect signal with this template:
<code>
<code>
QObject::connect ( const QObject * sender, const char * signal, const QObject * receiver, const char * method);
QObject::connect (
  const QObject * sender,
  const char * signal,
  const QObject * receiver,
  const char * method
);
</code>
</code>
You have to wrap ''const char * signal'' and ''const char * method'' into SIGNAL () and SLOT() macros.
 
You have to wrap <tt>const char * signal</tt> and <tt>const char * method</tt> into SIGNAL() and SLOT() macros.


And you also can disconnect signal-slot:
And you also can disconnect signal-slot:
<code>
<code>
QObject::disconnect ( const QObject * sender, const char * signal, const QObject * receiver, const char * method);
QObject::disconnect (
  const QObject * sender,
  const char * signal,
  const QObject * receiver,
  const char * method
);
</code>
</code>


== Deeper ==
== Deeper ==


Widgets emit signals when events occur. For example, a button will emit a "clicked" signal when it is clicked. A developer can choose to connect to a signal by creating a function (a "slot") and calling the <code>connect()</code> function to relate the signal to the slot. Qt's signals and slots mechanism does not require classes to have knowledge of each other, which makes it much easier to develop highly reusable classes. Since signals and slots are type-safe, type errors are reported as warnings and do not cause crashes to occur.
Widgets emit signals when events occur. For example, a button will emit a ''clicked'' signal when it is clicked. A developer can choose to connect to a signal by creating a function (a ''slot'') and calling the <tt>connect()</tt> function to relate the signal to the slot. Qt's signals and slots mechanism does not require classes to have knowledge of each other, which makes it much easier to develop highly reusable classes. Since signals and slots are type-safe, type errors are reported as warnings and do not cause crashes to occur.


For example, if a Quit button's <code>clicked()</code> signal is connected to the application's <code>quit()</code> slot, a user's click on Quit makes the application terminate. In code, this is written as
For example, if a Quit button's <tt>clicked()</tt> signal is connected to the application's <tt>quit()</tt> slot, a user's click on Quit makes the application terminate. In code, this is written as


<code>connect(button, SIGNAL (clicked()), qApp, SLOT (quit()));<code>
<code>
connect(button, SIGNAL (clicked()), qApp, SLOT (quit()));
</code>


Connections can be added or removed at any time during the execution of a Qt application, they can be set up so that they are executed when a signal is emitted or queued for later execution, and they can be made between objects in different threads.
Connections can be added or removed at any time during the execution of a Qt application, they can be set up so that they are executed when a signal is emitted or queued for later execution, and they can be made between objects in different threads.


The signals and slots mechanism is implemented in standard C+''. The implementation uses the C''+ preprocessor and <code>moc</code>, the Meta-Object Compiler, included with Qt. Code generation is performed automatically by sub:Qt's-Build-System. Developers never have to edit or even look at the generated code.
The signals and slots mechanism is implemented in standard C++. The implementation uses the C++ preprocessor and <tt>moc</tt>, the [http://doc.qt.io/qt-5/moc.html Meta Object Compiler], included with Qt. Code generation is performed automatically by Qt's build system. Developers never have to edit or even look at the generated code.


In addition to handling signals and slots, the Meta-Object Compiler supports Qt's translation mechanism, its property system, and its extended run-time type information. It also makes run-time introspection of C++ programs possible in a way that works on all supported platforms.
In addition to handling signals and slots, the Meta Object Compiler supports Qt's translation mechanism, its property system, and its extended runtime type information. It also makes runtime introspection of C++ programs possible in a way that works on all supported platforms.


== See also ==
To make moc compile the meta object classes don't forget to add the <tt>Q_OBJECT</tt> macro to your class.

Latest revision as of 22:19, 14 March 2015

Introduction

Remember old X-Windows call-back system? Generally it isn't type safe and flexible. There are many problems with them. Qt offers a new event handling system: signal-slot connections. Imagine an alarm clock. When alarm is ringing, a signal is being sent (emit). And you're handling it in a slot.

  • Every QObject class may have as many signals and slots as you want
  • You can emit signals only from within that class, where the signal is located
  • You can connect signal with another signal (make chains of signals);
  • Every signal and slot can have unlimited count of connections with other.
  • ATTENTION! You can't set default value in slot attributes e.g. void mySlot(int i = 0);

Connection

You can connect signal with this template:

QObject::connect (
  const QObject * sender,
  const char * signal,
  const QObject * receiver,
  const char * method
);

You have to wrap const char * signal and const char * method into SIGNAL() and SLOT() macros.

And you also can disconnect signal-slot:

QObject::disconnect (
  const QObject * sender,
  const char * signal,
  const QObject * receiver,
  const char * method
);

Deeper

Widgets emit signals when events occur. For example, a button will emit a clicked signal when it is clicked. A developer can choose to connect to a signal by creating a function (a slot) and calling the connect() function to relate the signal to the slot. Qt's signals and slots mechanism does not require classes to have knowledge of each other, which makes it much easier to develop highly reusable classes. Since signals and slots are type-safe, type errors are reported as warnings and do not cause crashes to occur.

For example, if a Quit button's clicked() signal is connected to the application's quit() slot, a user's click on Quit makes the application terminate. In code, this is written as

connect(button, SIGNAL (clicked()), qApp, SLOT (quit()));

Connections can be added or removed at any time during the execution of a Qt application, they can be set up so that they are executed when a signal is emitted or queued for later execution, and they can be made between objects in different threads.

The signals and slots mechanism is implemented in standard C++. The implementation uses the C++ preprocessor and moc, the Meta Object Compiler, included with Qt. Code generation is performed automatically by Qt's build system. Developers never have to edit or even look at the generated code.

In addition to handling signals and slots, the Meta Object Compiler supports Qt's translation mechanism, its property system, and its extended runtime type information. It also makes runtime introspection of C++ programs possible in a way that works on all supported platforms.

To make moc compile the meta object classes don't forget to add the Q_OBJECT macro to your class.