- Qt Connect Signal Slot
- Qt Signal Slot Reference Chart
- Qt Signal Slot Reference Tool
- Qt Connect Slot
- Qt Signal Slot Reference
- Qt Signal Slot Example
This section describes the new style of connecting signals and slotsintroduced in PyQt4 v4.5.
Passing a reference to a Qt signal is not dangerous thanks to the way signal/slot connections work: If the connection is direct, connected slots are directly called directly, e.g. When emit MySignal(mystring) returns all directly connected slots have been executed. If the the connection is queued, Qt creates a copy of the referencees.
One of the key features of Qt is its use of signals and slots to communicatebetween objects. Their use encourages the development of reusable components.
Use the method sender available in QObject to get the reference of the sender inside the slot. Example - qt signal slot reference. Before signal slot called after signal 予想. PyQt 4.11.4 Reference Guide » New-style Signal and Slot Support¶ This section describes the new style of connecting signals and slots introduced in PyQt4 v4.5. One of the key features of Qt is its use of signals and slots to communicate between objects. The Signal/Slot Editor. The signal and slot used in a connection can be changed after it has been set up. When a connection is configured, it becomes visible in Qt Designer's signal and slot editor where it can be further edited. You can also edit signal/slot connections by double-clicking on the connection path or one of its labels to display.
A signal is emitted when something of potential interest happens. A slot is aPython callable. If a signal is connected to a slot then the slot is calledwhen the signal is emitted. If a signal isn't connected then nothing happens.The code (or component) that emits the signal does not know or care if thesignal is being used.
The signal/slot mechanism has the following features.
Qt Connect Signal Slot
- A signal may be connected to many slots.
- A signal may also be connected to another signal.
- Signal arguments may be any Python type.
- A slot may be connected to many signals.
- Connections may be direct (ie. synchronous) or queued (ie. asynchronous).
- Connections may be made across threads.
- Signals may be disconnected.
Unbound and Bound Signals¶
A signal (specifically an unbound signal) is an attribute of a class that is asub-class of QObject
. When a signal is referenced as an attribute of aninstance of the class then PyQt4 automatically binds the instance to the signalin order to create a bound signal. This is the same mechanism that Pythonitself uses to create bound methods from class functions.
A bound signal has connect()
, disconnect()
and emit()
methods thatimplement the associated functionality. It also has a signal
attributethat is the signature of the signal that would be returned by Qt's SIGNAL()
macro.
A signal may be overloaded, ie. a signal with a particular name may supportmore than one signature. A signal may be indexed with a signature in order toselect the one required. A signature is a sequence of types. A type is eithera Python type object or a string that is the name of a C++ type. The name of aC++ type is automatically normalised so that, for example, QString
can beused instead of the non-normalised constQString&
.
If a signal is overloaded then it will have a default that will be used if noindex is given.
When a signal is emitted then any arguments are converted to C++ types ifpossible. If an argument doesn't have a corresponding C++ type then it iswrapped in a special C++ type that allows it to be passed around Qt's meta-typesystem while ensuring that its reference count is properly maintained.
Defining New Signals with pyqtSignal()
¶
PyQt4 automatically defines signals for all Qt's built-in signals. New signalscan be defined as class attributes using the pyqtSignal()
factory.
PyQt4.QtCore.
pyqtSignal
(types[, name])¶Create one or more overloaded unbound signals as a class attribute.
Parameters: |
|
---|---|
Return type: | an unbound signal |
The following example shows the definition of a number of new signals:
New signals should only be defined in sub-classes of QObject
. They must bepart of the class definition and cannot be dynamically added as classattributes after the class has been defined.
New signals defined in this way will be automatically added to the class'sQMetaObject
. This means that they will appear in Qt Designer and can beintrospected using the QMetaObject
API.
Overloaded signals should be used with care when an argument has a Python typethat has no corresponding C++ type. PyQt4 uses the same internal C++ class torepresent such objects and so it is possible to have overloaded signals withdifferent Python signatures that are implemented with identical C++ signatureswith unexpected results. The following is an example of this:
Connecting, Disconnecting and Emitting Signals¶
Signals are connected to slots using the connect()
method of a boundsignal.
connect
(slot[, type=PyQt4.QtCore.Qt.AutoConnection[, no_receiver_check=False]])¶Connect a signal to a slot. An exception will be raised if the connectionfailed.
Parameters: |
|
---|
Signals are disconnected from slots using the disconnect()
method of abound signal.
disconnect
([slot])¶Disconnect one or more slots from a signal. An exception will be raised ifthe slot is not connected to the signal or if the signal has no connectionsat all.
Parameters: | slot – the optional slot to disconnect from, either a Python callable oranother bound signal. If it is omitted then all slots connected to thesignal are disconnected. |
---|
Signals are emitted from using the emit()
method of a bound signal.
Carte Blanche Casino & Entertainment 1 Photo Booth Rentals, Party Equipment Rentals. Browse our selection of 1 Casino hotels & resorts in Dallas, TX for the ultimate stay & play vacation. Make your casino vacation a sure bet with Expedia and save your money & time.
emit
(*args)¶Emit a signal.
Parameters: | args – the optional sequence of arguments to pass to any connected slots. |
---|
The following code demonstrates the definition, connection and emit of asignal without arguments:
The following code demonstrates the connection of overloaded signals:
Connecting Signals Using Keyword Arguments¶
It is also possible to connect signals by passing a slot as a keyword argumentcorresponding to the name of the signal when creating an object, or using thepyqtConfigure()
method of QObject
. For example the following threefragments are equivalent:
The pyqtSlot()
Decorator¶
Although PyQt4 allows any Python callable to be used as a slot when connectingsignals, it is sometimes necessary to explicitly mark a Python method as beinga Qt slot and to provide a C++ signature for it. PyQt4 provides thepyqtSlot()
function decorator to do this.
PyQt4.QtCore.
pyqtSlot
(types[, name[, result]])¶Decorate a Python method to create a Qt slot.
Parameters: |
|
---|
Connecting a signal to a decorated Python method also has the advantage ofreducing the amount of memory used and is slightly faster.
For example:
It is also possible to chain the decorators in order to define a Python methodseveral times with different signatures. For example:
Connecting Slots By Name¶
PyQt4 supports the QtCore.QMetaObject.connectSlotsByName()
function thatis most commonly used by pyuic4 generated Python code toautomatically connect signals to slots that conform to a simple namingconvention. However, where a class has overloaded Qt signals (ie. with thesame name but with different arguments) PyQt4 needs additional information inorder to automatically connect the correct signal.
For example the QtGui.QSpinBox
class has the following signals:
When the value of the spin box changes both of these signals will be emitted.If you have implemented a slot called on_spinbox_valueChanged
(whichassumes that you have given the QSpinBox
instance the name spinbox
)then it will be connected to both variations of the signal. Therefore, whenthe user changes the value, your slot will be called twice - once with aninteger argument, and once with a unicode or QString
argument.
This also happens with signals that take optional arguments. Qt implementsthis using multiple signals. For example, QtGui.QAbstractButton
has thefollowing signal:
A signal (specifically an unbound signal) is an attribute of a class that is asub-class of QObject
. When a signal is referenced as an attribute of aninstance of the class then PyQt4 automatically binds the instance to the signalin order to create a bound signal. This is the same mechanism that Pythonitself uses to create bound methods from class functions.
A bound signal has connect()
, disconnect()
and emit()
methods thatimplement the associated functionality. It also has a signal
attributethat is the signature of the signal that would be returned by Qt's SIGNAL()
macro.
A signal may be overloaded, ie. a signal with a particular name may supportmore than one signature. A signal may be indexed with a signature in order toselect the one required. A signature is a sequence of types. A type is eithera Python type object or a string that is the name of a C++ type. The name of aC++ type is automatically normalised so that, for example, QString
can beused instead of the non-normalised constQString&
.
If a signal is overloaded then it will have a default that will be used if noindex is given.
When a signal is emitted then any arguments are converted to C++ types ifpossible. If an argument doesn't have a corresponding C++ type then it iswrapped in a special C++ type that allows it to be passed around Qt's meta-typesystem while ensuring that its reference count is properly maintained.
Defining New Signals with pyqtSignal()
¶
PyQt4 automatically defines signals for all Qt's built-in signals. New signalscan be defined as class attributes using the pyqtSignal()
factory.
PyQt4.QtCore.
pyqtSignal
(types[, name])¶Create one or more overloaded unbound signals as a class attribute.
Parameters: |
|
---|---|
Return type: | an unbound signal |
The following example shows the definition of a number of new signals:
New signals should only be defined in sub-classes of QObject
. They must bepart of the class definition and cannot be dynamically added as classattributes after the class has been defined.
New signals defined in this way will be automatically added to the class'sQMetaObject
. This means that they will appear in Qt Designer and can beintrospected using the QMetaObject
API.
Overloaded signals should be used with care when an argument has a Python typethat has no corresponding C++ type. PyQt4 uses the same internal C++ class torepresent such objects and so it is possible to have overloaded signals withdifferent Python signatures that are implemented with identical C++ signatureswith unexpected results. The following is an example of this:
Connecting, Disconnecting and Emitting Signals¶
Signals are connected to slots using the connect()
method of a boundsignal.
connect
(slot[, type=PyQt4.QtCore.Qt.AutoConnection[, no_receiver_check=False]])¶Connect a signal to a slot. An exception will be raised if the connectionfailed.
Parameters: |
|
---|
Signals are disconnected from slots using the disconnect()
method of abound signal.
disconnect
([slot])¶Disconnect one or more slots from a signal. An exception will be raised ifthe slot is not connected to the signal or if the signal has no connectionsat all.
Parameters: | slot – the optional slot to disconnect from, either a Python callable oranother bound signal. If it is omitted then all slots connected to thesignal are disconnected. |
---|
Signals are emitted from using the emit()
method of a bound signal.
Carte Blanche Casino & Entertainment 1 Photo Booth Rentals, Party Equipment Rentals. Browse our selection of 1 Casino hotels & resorts in Dallas, TX for the ultimate stay & play vacation. Make your casino vacation a sure bet with Expedia and save your money & time.
emit
(*args)¶Emit a signal.
Parameters: | args – the optional sequence of arguments to pass to any connected slots. |
---|
The following code demonstrates the definition, connection and emit of asignal without arguments:
The following code demonstrates the connection of overloaded signals:
Connecting Signals Using Keyword Arguments¶
It is also possible to connect signals by passing a slot as a keyword argumentcorresponding to the name of the signal when creating an object, or using thepyqtConfigure()
method of QObject
. For example the following threefragments are equivalent:
The pyqtSlot()
Decorator¶
Although PyQt4 allows any Python callable to be used as a slot when connectingsignals, it is sometimes necessary to explicitly mark a Python method as beinga Qt slot and to provide a C++ signature for it. PyQt4 provides thepyqtSlot()
function decorator to do this.
PyQt4.QtCore.
pyqtSlot
(types[, name[, result]])¶Decorate a Python method to create a Qt slot.
Parameters: |
|
---|
Connecting a signal to a decorated Python method also has the advantage ofreducing the amount of memory used and is slightly faster.
For example:
It is also possible to chain the decorators in order to define a Python methodseveral times with different signatures. For example:
Connecting Slots By Name¶
PyQt4 supports the QtCore.QMetaObject.connectSlotsByName()
function thatis most commonly used by pyuic4 generated Python code toautomatically connect signals to slots that conform to a simple namingconvention. However, where a class has overloaded Qt signals (ie. with thesame name but with different arguments) PyQt4 needs additional information inorder to automatically connect the correct signal.
For example the QtGui.QSpinBox
class has the following signals:
When the value of the spin box changes both of these signals will be emitted.If you have implemented a slot called on_spinbox_valueChanged
(whichassumes that you have given the QSpinBox
instance the name spinbox
)then it will be connected to both variations of the signal. Therefore, whenthe user changes the value, your slot will be called twice - once with aninteger argument, and once with a unicode or QString
argument.
This also happens with signals that take optional arguments. Qt implementsthis using multiple signals. For example, QtGui.QAbstractButton
has thefollowing signal:
Qt implements this as the following:
The pyqtSlot()
decorator can be used to specify which ofthe signals should be connected to the slot.
For example, if you were only interested in the integer variant of the signalthen your slot definition would look like the following:
If you wanted to handle both variants of the signal, but with different Pythonmethods, then your slot definitions might look like the following:
Qt Signal Slot Reference Chart
The following shows an example using a button when you are not interested inthe optional argument:
Mixing New-style and Old-style Connections¶
The implementation of new-style connections is slightly different to theimplementation of old-style connections. An application can freely use bothstyles subject to the restriction that any individual new-style connectionshould only be disconnected using the new style. Similarly any individualold-style connection should only be disconnected using the old style.
You should also be aware that pyuic4 generates code that usesold-style connections.
EnArBgDeElEsFaFiFrHiHuItJaKnKoMsNlPlPtRuSqThTrUkZh
Welcome to the Qt wiki
Here the Qt community has gathered information on Qt over the years.
Qt Signal Slot Reference Tool
Everything here is created and maintained by the community.
Please take a look at the below information before you start contributing.
|
Editing Info
- MediaWiki help on editing pages: Comprehensive guides on wiki editing in general.
- Wiki Editing Hints: Tips on how to edit this wiki.
- Articles needing cleanup: A list of articles that might need polishing.
Quick Access (Portal)
Current Releases (All Releases) | |||
---|---|---|---|
Qt Version | State | New Features | Download Online Installers (All Downloads) |
Linux (32 bit) | Linux (64 bit) | Mac | Windows |
Qt 4.8.7 | End-of-Life (as of December 2015) | Go to download directory | |
Qt 5.12 | Long Term Support Release | New Features in Qt 5.12 | - |
Qt 5.15 | Standard Support Long Term Support Release | New Features in Qt 5.15 | - |
Qt 6.0 | Development | New Features in Qt 6.0 | - |
New to Qt 5 |
|
Application Developers (Portal)
Qt Creator | QtQuick | XML | JSON | Network | Web | Serial Port | Database | |
Localization | Multimedia | Bluetooth | Language Bindings | Widgets | 2D | Threads | Deploying | |
Tools | Add-ons | Porting | Plugins | Testing | Printing | 3D | Sensors |
Desktop (Portal) | Mobile (Portal) |
Contributors (Portal)
- Security: Reporting security issues and the Qt Project Security Policy
- Qt Project Guidelines Guides and useful information for code contributors to the Qt Project
- Guidelines: General guidelines regarding Qt code (excl. those specifically for code contributions to the Qt Project)
- Instructions: Instructions for set-up and tools
- Documentation: Resources regarding writing documentation and examples
Qt Connect Slot
Special Interest (Portal)
Qt Signal Slot Reference
- Language bindings (Overview)