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
 
No edit summary
Line 1: Line 1:
'''English''' [[How to Use Signals and Slots SimplifiedChinese|简体中文]]
[[Category:Developing with Qt::General]]<br />[[Category:HowTo]]<br />[[Category:Snippets]]<br />[[Category:Tutorial]]<br />[[Category:Learning]]


=How to Use Signals and Slots?=
[toc align_right=&quot;yes&amp;quot; depth=&quot;3&amp;quot;]


==Introduction==
'''English''' [[How_to_Use_Signals_and_Slots_SimplifiedChinese|简体中文]]


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.
= How to Use Signals and Slots? =
 
== Introduction ==
 
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 of slots as you want.
* Every QObject class may have as many signals of slots as you want.
Line 11: Line 15:
* 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.
* '''<span class="caps">ATTENTION</span>!''' 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.'' &lt;code&amp;gt;void mySlot(int i = 0);&lt;/code&amp;gt;
 
== Connection ==


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


You can connect signal with this template:<br /> You have to wrap ''const char * signal'' and ''const char * method'' into <span class="caps">SIGNAL</span> and <span class="caps">SLOT</span> macros.
And you also can disconnect signal-slot:<br /><code><br />QObject::disconnect ( const QObject * sender, const char * signal, const QObject * receiver, const char * method);<br /></code>


And you also can disconnect signal-slot:<br />
== Deeper ==


==Deeper==
Widgets emit signals when events occur. For example, a button will emit a &quot;clicked&amp;quot; signal when it is clicked. A developer can choose to connect to a signal by creating a function (a &quot;slot&amp;quot;) and calling the &lt;code&amp;gt;connect()&lt;/code&amp;gt; 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 <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.
For example, if a Quit button's &lt;code&amp;gt;clicked()&lt;/code&amp;gt; signal is connected to the application's &lt;code&amp;gt;quit()&lt;/code&amp;gt; slot, a user's click on Quit makes the application terminate. In code, this is written as


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
<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 &lt;code&amp;gt;moc&amp;lt;/code&amp;gt;, 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.
 
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.
 
==See also==
 
[http://blog.csdn.net/qter_wd007/article/details/5598264 Qt中的信号与槽详解] ''[blog.csdn.net]''


===Categories:===
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.


* [[:Category:Developing-with-Qt|Developing with Qt]]
== See also ==
** [[:Category:Developing-with-Qt::General|General]]
* [[:Category:HowTo|HowTo]]
* [[:Category:Learning|Learning]]
* [[:Category:snippets|snippets]]
* [[:Category:Tutorial|Tutorial]]

Revision as of 09:47, 24 February 2015





[toc align_right="yes&quot; depth="3&quot;]

English 简体中文

How to Use Signals and Slots?

Introduction

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 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);
  • 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&gt;void mySlot(int i = 0);</code&gt;

Connection

You can connect signal with this template:

<br />QObject::connect ( const QObject * sender, const char * signal, const QObject * receiver, const char * method);<br />


You have to wrap const char * signal and const char * method into SIGNAL () and SLOT() macros. And you also can disconnect signal-slot:

<br />QObject::disconnect ( const QObject * sender, const char * signal, const QObject * receiver, const char * method);<br />

Deeper

Widgets emit signals when events occur. For example, a button will emit a "clicked&quot; signal when it is clicked. A developer can choose to connect to a signal by creating a function (a "slot&quot;) and calling the <code&gt;connect()</code&gt; 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&gt;clicked()</code&gt; signal is connected to the application's <code&gt;quit()</code&gt; 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 <code&gt;moc&lt;/code&gt;, 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.

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.

See also