Signals and slots are loosely coupled: a class which emits a signal neither knows nor cares which slots receive the signal. Qt's signals and slots mechanism ensures that if you connect a signal to a slot, the slot will be called with the signal's parameters at the right time. Signals and slots can take any number of arguments of any type. The connection mechanism uses a vector indexed by signals. But all the slots waste space in the vector and there are usually more slots than signals in an object. So from Qt 4.6, a new internal signal index which only includes the signal index is used. While developing with Qt, you only need to know about the absolute method index.
PrevNextTable of Contents
The most important features of Qt are signals and slots.
Signals tell you that something has just happened. Signals are emitted (sent) when the user works with the computer. For example, when the user clicks the mouse or presses keys on a keyboard a signal is emitted. Signals can also be emitted when something happens inside the computer—when the clock ticks, for example.
Slots are the functions that respond to certain signals. It is important that your program responds to signals. Otherwise, it might look as if your program hangs. KDE programs don't—or shouldn't—hang!
Signals and slots are very object independent. Slots that handle a signal can be put in any object in your program. The object that sends the signal doesn't have to know anything about the slot or the object where the slot can be found. For example, you may have one window that contains a button and one window that contains a text box. You can let the text box respond to button clicks.
Signals and slots are primarily used for events handling, but you can use it for easy communication between objects too. When two windows need to communicate with each other, you can use signals and slots. Communication this way is much easier than doing it with pointers.
Event handling is solved by callbacks in many other toolkits. A callback is a pointer to a function. The widgets contain callbacks, pointers to functions, for each event. When an event occurs, the appropriate function is called. It is simple in theory, but it is hard in practice. The callbacks are not type safe, which means that it is easy to make mistakes. Callbacks also can't take any number of parameters of any type like signals and slots do.
3.3.1. Creating a SlotCreating a slot is easy. Any class that inherits from QObject can have slots.
First you must enable signals and slots. In the class definition, add the word Q_OBJECT. This is a keyword, which the moc understands.
The slot is just a member function in your class, but you must declare it in a slots section. Slots can be public, private, or protected.
The following example shows a class with a slot:
The slot in the preceding class definition is called mySlot. The keyword before slots defines the access mode. The slot mySlot above is public.
You write the implementation for the slot as if it was a common member function. The following example shows you what a slot implementation may look like:
3.3.2. Emitting a SignalWhen you want to tell Qt that an event has occurred, you emit a signal. When that happens, Qt executes all slots that are connected to the signal.
Before a signal can be emitted, it must be defined. The class that emits a signal must contain the signal definition. Signals are defined in a signals section in your class. The following class definition defines a signal:
Signals are emitted with the command emit. The signal may be emitted like so:
The example above is only a simple demonstration that shows you how it works.
3.3.3. Connecting a Slot to a SignalTo make a slot respond to a certain signal, you must connect them to each other. You can connect several slots to one signal.
It is very simple to connect a slot to a signal. The command connect does this. The syntax is simple:
The parameter startobject contains a pointer to the object that the signal comes from.
The parameter signal specifies what signal to handle. The signal must be emitted by the startobject.
The object which responds to a signal is specified in the parameter targetobject.
The slot which responds to the signal is specified in the parameter slot. The slot must be in the object specified by targetobject.
The following class demonstrates that several slots can be connected to the same signal, and one slot can be connected to several signals:
Example 3.4. buttons.h: Class Definition for the Class MyWindow
The listing below contains the class implementation:
Example 3.5. buttons.cc: Class Implementation for the Class MyWindow Declared in Listing 3.4
3.3.4. Signals and Slots with ParametersDuring communication, it is sometimes useful to say more than 'Hey!' That is all that the preceding signals say.
If you need to say more, the simplest way is to use parameters in your signals and slots.
For example, you may have two windows both containing a button and a text box. When the user types in text and clicks the button in one window, the caption for the other window will change to whatever was typed in.
The solution is to use slots and signals with parameters. Give both the signal and slot a parameter that contains the new window caption. When you emit the signal you set this parameter.
The following example code shows how parameters work. The signal and slot are both in the same class, but of course that is not necessary:
The class constructor may connect the slot to the signal, like this:
The slot and the signal must have compatible parameters. In the preceding example, they each have one integer as a parameter.
It is easy to emit a signal with a parameter. The following function emits the signal changed(int i):
3.3.5. Slots in Temporary ClassesWhen a signal is emitted, the slots connected to it are activated.
Take a look at the following class constructor:
A button is created. The clicked() signal is connected to temp->slotTemp(). When you delete temp, the slot slotTemp() is also deleted. If the user clicks the button, an error will occur. Always consider this when you delete Qt objects.
PrevNextTable of Contents
PrevNextTable of Contents
The most important features of Qt are signals and slots.
Signals tell you that something has just happened. Signals are emitted (sent) when the user works with the computer. For example, when the user clicks the mouse or presses keys on a keyboard a signal is emitted. Signals can also be emitted when something happens inside the computer—when the clock ticks, for example.
Slots are the functions that respond to certain signals. It is important that your program responds to signals. Otherwise, it might look as if your program hangs. KDE programs don't—or shouldn't—hang!
Signals and slots are very object independent. Slots that handle a signal can be put in any object in your program. The object that sends the signal doesn't have to know anything about the slot or the object where the slot can be found. For example, you may have one window that contains a button and one window that contains a text box. You can let the text box respond to button clicks.
Signals and slots are primarily used for events handling, but you can use it for easy communication between objects too. When two windows need to communicate with each other, you can use signals and slots. Communication this way is much easier than doing it with pointers.
Event handling is solved by callbacks in many other toolkits. A callback is a pointer to a function. The widgets contain callbacks, pointers to functions, for each event. When an event occurs, the appropriate function is called. It is simple in theory, but it is hard in practice. The callbacks are not type safe, which means that it is easy to make mistakes. Callbacks also can't take any number of parameters of any type like signals and slots do.
3.3.1. Creating a SlotCreating a slot is easy. Any class that inherits from QObject can have slots.
First you must enable signals and slots. In the class definition, add the word Q_OBJECT. This is a keyword, which the moc understands.
The slot is just a member function in your class, but you must declare it in a slots section. Slots can be public, private, or protected.
The following example shows a class with a slot:
The slot in the preceding class definition is called mySlot. The keyword before slots defines the access mode. The slot mySlot above is public.
You write the implementation for the slot as if it was a common member function. The following example shows you what a slot implementation may look like:
3.3.2. Emitting a SignalWhen you want to tell Qt that an event has occurred, you emit a signal. When that happens, Qt executes all slots that are connected to the signal.
Slots are the functions that respond to certain signals. It is important that your program responds to signals. Otherwise, it might look as if your program hangs. KDE programs don't—or shouldn't—hang!
Signals and slots are very object independent. Slots that handle a signal can be put in any object in your program. The object that sends the signal doesn't have to know anything about the slot or the object where the slot can be found. For example, you may have one window that contains a button and one window that contains a text box. You can let the text box respond to button clicks.
Signals and slots are primarily used for events handling, but you can use it for easy communication between objects too. When two windows need to communicate with each other, you can use signals and slots. Communication this way is much easier than doing it with pointers.
Event handling is solved by callbacks in many other toolkits. A callback is a pointer to a function. The widgets contain callbacks, pointers to functions, for each event. When an event occurs, the appropriate function is called. It is simple in theory, but it is hard in practice. The callbacks are not type safe, which means that it is easy to make mistakes. Callbacks also can't take any number of parameters of any type like signals and slots do.
3.3.1. Creating a SlotCreating a slot is easy. Any class that inherits from QObject can have slots.
First you must enable signals and slots. In the class definition, add the word Q_OBJECT. This is a keyword, which the moc understands.
The slot is just a member function in your class, but you must declare it in a slots section. Slots can be public, private, or protected.
The following example shows a class with a slot:
The slot in the preceding class definition is called mySlot. The keyword before slots defines the access mode. The slot mySlot above is public.
You write the implementation for the slot as if it was a common member function. The following example shows you what a slot implementation may look like:
3.3.2. Emitting a SignalWhen you want to tell Qt that an event has occurred, you emit a signal. When that happens, Qt executes all slots that are connected to the signal.
Before a signal can be emitted, it must be defined. The class that emits a signal must contain the signal definition. Signals are defined in a signals section in your class. The following class definition defines a signal:
Signals are emitted with the command emit. The signal may be emitted like so:
The example above is only a simple demonstration that shows you how it works.
3.3.3. Connecting a Slot to a SignalTo make a slot respond to a certain signal, you must connect them to each other. You can connect several slots to one signal.
It is very simple to connect a slot to a signal. The command connect does this. The syntax is simple:
The parameter startobject contains a pointer to the object that the signal comes from.
The parameter signal specifies what signal to handle. The signal must be emitted by the startobject.
The object which responds to a signal is specified in the parameter targetobject.
The slot which responds to the signal is specified in the parameter slot. The slot must be in the object specified by targetobject.
The following class demonstrates that several slots can be connected to the same signal, and one slot can be connected to several signals:
Example 3.4. buttons.h: Class Definition for the Class MyWindow
The listing below contains the class implementation:
Example 3.5. buttons.cc: Class Implementation for the Class MyWindow Declared in Listing 3.4
3.3.4. Signals and Slots with ParametersDuring communication, it is sometimes useful to say more than 'Hey!' That is all that the preceding signals say.
If you need to say more, the simplest way is to use parameters in your signals and slots.
For example, you may have two windows both containing a button and a text box. When the user types in text and clicks the button in one window, the caption for the other window will change to whatever was typed in.
The solution is to use slots and signals with parameters. Give both the signal and slot a parameter that contains the new window caption. When you emit the signal you set this parameter.
The following example code shows how parameters work. The signal and slot are both in the same class, but of course that is not necessary:
The class constructor may connect the slot to the signal, like this:
The slot and the signal must have compatible parameters. In the preceding example, they each have one integer as a parameter.
It is easy to emit a signal with a parameter. The following function emits the signal changed(int i):
3.3.5. Slots in Temporary ClassesWhen a signal is emitted, the slots connected to it are activated.
Take a look at the following class constructor:
A button is created. The clicked() signal is connected to temp->slotTemp(). When you delete temp, the slot slotTemp() is also deleted. If the user clicks the button, an error will occur. Always consider this when you delete Qt objects.
PrevNextTable of Contents
PrevNextTable of Contents
The most important features of Qt are signals and slots.
Signals tell you that something has just happened. Signals are emitted (sent) when the user works with the computer. For example, when the user clicks the mouse or presses keys on a keyboard a signal is emitted. Signals can also be emitted when something happens inside the computer—when the clock ticks, for example.
Slots are the functions that respond to certain signals. It is important that your program responds to signals. Otherwise, it might look as if your program hangs. KDE programs don't—or shouldn't—hang!
Signals and slots are very object independent. Slots that handle a signal can be put in any object in your program. The object that sends the signal doesn't have to know anything about the slot or the object where the slot can be found. For example, you may have one window that contains a button and one window that contains a text box. You can let the text box respond to button clicks.
Signals and slots are primarily used for events handling, but you can use it for easy communication between objects too. When two windows need to communicate with each other, you can use signals and slots. Communication this way is much easier than doing it with pointers.
Event handling is solved by callbacks in many other toolkits. A callback is a pointer to a function. The widgets contain callbacks, pointers to functions, for each event. When an event occurs, the appropriate function is called. It is simple in theory, but it is hard in practice. The callbacks are not type safe, which means that it is easy to make mistakes. Callbacks also can't take any number of parameters of any type like signals and slots do.
3.3.1. Creating a SlotCreating a slot is easy. Any class that inherits from QObject can have slots.
First you must enable signals and slots. In the class definition, add the word Q_OBJECT. This is a keyword, which the moc understands.
The slot is just a member function in your class, but you must declare it in a slots section. Slots can be public, private, or protected.
The following example shows a class with a slot:
The slot in the preceding class definition is called mySlot. The keyword before slots defines the access mode. The slot mySlot above is public.
You write the implementation for the slot as if it was a common member function. The following example shows you what a slot implementation may look like:
3.3.2. Emitting a SignalWhen you want to tell Qt that an event has occurred, you emit a signal. When that happens, Qt executes all slots that are connected to the signal.
Before a signal can be emitted, it must be defined. The class that emits a signal must contain the signal definition. Signals are defined in a signals section in your class. The following class definition defines a signal:
Signals are emitted with the command emit. The signal may be emitted like so:
The example above is only a simple demonstration that shows you how it works.
3.3.3. Connecting a Slot to a SignalTo make a slot respond to a certain signal, you must connect them to each other. You can connect several slots to one signal.
It is very simple to connect a slot to a signal. The command connect does this. The syntax is simple:
The parameter startobject contains a pointer to the object that the signal comes from.
The parameter signal specifies what signal to handle. The signal must be emitted by the startobject.
The object which responds to a signal is specified in the parameter targetobject.
The slot which responds to the signal is specified in the parameter slot. The slot must be in the object specified by targetobject.
The following class demonstrates that several slots can be connected to the same signal, and one slot can be connected to several signals:
Example 3.4. buttons.h: Class Definition for the Class MyWindow
The listing below contains the class implementation:
Example 3.5. buttons.cc: Class Implementation for the Class MyWindow Declared in Listing 3.4
3.3.4. Signals and Slots with ParametersDuring communication, it is sometimes useful to say more than 'Hey!' That is all that the preceding signals say.
If you need to say more, the simplest way is to use parameters in your signals and slots.
For example, you may have two windows both containing a button and a text box. When the user types in text and clicks the button in one window, the caption for the other window will change to whatever was typed in.
Qt Signal Slot Call Orders
The solution is to use slots and signals with parameters. Give both the signal and slot a parameter that contains the new window caption. When you emit the signal you set this parameter.
The following example code shows how parameters work. The signal and slot are both in the same class, but of course that is not necessary:
The class constructor may connect the slot to the signal, like this:
The slot and the signal must have compatible parameters. In the preceding example, they each have one integer as a parameter.
It is easy to emit a signal with a parameter. The following function emits the signal changed(int i):
Qt Signal Slot Call Ordering
3.3.5. Slots in Temporary ClassesWhen a signal is emitted, the slots connected to it are activated.
Take a look at the following class constructor:
A button is created. The clicked() signal is connected to temp->slotTemp(). When you delete temp, the slot slotTemp() is also deleted. If the user clicks the button, an error will occur. Always consider this when you delete Qt objects.
PrevNextTable of Contents