- Qt Signal Slot Not Working
- How Qt Signal And Slots Works
- Qt Signal Slot Parameter
- Qt Signal Slot Same Classic
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. Signals and slots were one of the distinguishing features that made Qt an exciting and innovative tool back in time. But sometimes you can teach new tricks to an old dog, and QObjects gained a new way to connect between signals and slots in Qt5, plus some extra features to connect to other functions which are not slots.
This section describes the older style for connecting signals and slots. Ituses the same API that a C++ application would use. This has a number ofadvantages.
- It is well understood and documented.
- Any future changes to the C++ API should be easily included.
It also has a number of disadvantages.
- It requires knowledge of the C++ types of signal arguments.
- It is error prone in that if you mis-type the signal name or signature thenno exception is raised, either when the signal is connected or emitted.
- It is verbose.
- It is not Pythonic.
This older style of connecting signals and slots will continue to be supportedthroughout the life of PyQt4.
PyQt4 Signals and Qt Signals¶
Qt Signal Slot Not Working
Qt signals are statically defined as part of a C++ class. They are referencedusing the
QtCore.SIGNAL()
function. This method takes a single stringargument that is the name of the signal and its C++ signature. For example:The returned value is normally passed to the
QtCore.QObject.connect()
method.PyQt4 allows new signals to be defined dynamically. The act of emitting aPyQt4 signal implicitly defines it. PyQt4 signals are also referenced usingthe
QtCore.SIGNAL()
function.The PyQt_PyObject
Signal Argument Type¶
It is possible to pass any Python object as a signal argument by specifying
PyQt_PyObject
as the type of the argument in the signature. For example:While this would normally be used for passing objects like lists anddictionaries as signal arguments, it can be used for any Python type. Itsadvantage when passing, for example, an integer is that the normal conversionsfrom a Python object to a C++ integer and back again are not required.
The reference count of the object being passed is maintained automatically.There is no need for the emitter of a signal to keep a reference to the objectafter the call to
QtCore.QObject.emit()
, even if a connection is queued.Short-circuit Signals¶
There is also a special form of a PyQt4 signal known as a short-circuit signal.Short-circut signals implicitly declare each argument as being of type
PyQt_PyObject
.Short-circuit signals do not have a list of arguments or the surroundingparentheses.
Short-circuit signals may only be connected to slots that have been implementedin Python. They cannot be connected to Qt slots or the Python callables thatwrap Qt slots.
PyQt4 Slots and Qt Slots¶
Qt slots are statically defined as part of a C++ class. They are referencedusing the
QtCore.SLOT()
function. This method takes a single stringargument that is the name of the slot and its C++ signature. For example:The returned value is normally passed to the
QtCore.QObject.connect()
method.PyQt4 allows any Python callable to be used as a slot, not just Qt slots. Thisis done by simply referencing the callable. Because Qt slots are implementedas class methods they are also available as Python callables. Therefore it isnot usually necessary to use
QtCore.SLOT()
for Qt slots. However, doing sois more efficient as it avoids a conversion to Python and back to C++.Qt allows a signal to be connected to a slot that requires fewer arguments thanthe signal passes. The extra arguments are quietly discarded. PyQt4 slots canbe used in the same way.
Note that when a slot is a Python callable its reference count is notincreased. This means that a class instance can be deleted without having toexplicitly disconnect any signals connected to its methods. However, if a slotis a lambda function or a partial function then its reference count isautomatically incremented to prevent it from being immediately garbagecollected.
Connecting Signals and Slots¶
Connections between signals and slots (and other signals) are made using the
QtCore.QObject.connect()
method. For example:Disconnecting signals works in exactly the same way using the
QtCore.QObject.disconnect()
method. However, not all the variations ofthat method are supported by PyQt4. Signals must be disconnected one at atime.Emitting Signals¶
Any instance of a class that is derived from the
QtCore.QObject
class canemit a signal using its emit()
method. This takes a minimum of oneargument which is the signal. Any other arguments are passed to the connectedslots as the signal arguments. For example:The QtCore.pyqtSignature()
Decorator¶
The
QtCore.pyqtSignature()
serves the same purpose as thepyqtSlot()
decorator but has a less Pythonic API.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.
How Qt Signal And Slots Works
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.
Qt Signal Slot Parameter
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.
Qt Signal Slot Same Classic
PrevNextTable of Contents