Signal Slot Qt Python

Latest version

Released:

The Qt signals/slots and property system are based on the ability to introspect the objects at runtime. Introspection means being able to list the methods and properties of an object and have all kinds of information about them such as the type of their arguments. QtScript and QML would have hardly been possible without that ability. The receivers of signals are called Slots in Qt terminology. A number of standard slots are provided on Qt classes to allow you to wire together different parts of your application. However, you can also use any Python function as a slot, and therefore receive the message yourself.

A QT signal/slot concept for Python with Weakrefs.

Project description

QSignal – A QT Signal/slot for python

This project provides easy to use Signal class for implementing Signal/Slot mechanism in Python.It does not implement it strictly but rather creates the easy and simple alternative.

Classes

Slot

Signal

qsignal.Signal is the main class.

Slot

To create a signal, just make a sig = qsignal.Signal and set up an emitter of it. Or create it withsig = qsignal.Signal(emitter=foo).

To emit it, just call your sig().Or emit it in asynchronous mode with the method async.

Example:

To connect slots to it, pass callbacks into connect. The connections are maintained through weakref, thusyou don’t need to search for them and disconnect whenever you’re up to destroy some object.

Signaller

The base class for objects to maintain their Signals.

The only purpose of this class is to automate Signal names and emitter references.

Release historyRelease notifications RSS feed

1.0.2b1 pre-release

1.0.1b1 pre-release

1.0.0b1 pre-release

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for qsignal, version 1.0.2b1
Filename, sizeFile typePython versionUpload dateHashes
Filename, size qsignal-1.0.2b1-py2.py3-none-any.whl (6.7 kB) File type Wheel Python version py2.py3 Upload dateHashes
Filename, size qsignal-1.0.2b1.tar.gz (7.5 kB) File type Source Python version None Upload dateHashes
Close

Hashes for qsignal-1.0.2b1-py2.py3-none-any.whl

Hashes for qsignal-1.0.2b1-py2.py3-none-any.whl
AlgorithmHash digest
SHA25612f758d84e543bfc209fba81365a2777039a457d25427253996b0fb83d14ee1c
MD52a5fba356eb42d102b13290a0116ccea
BLAKE2-256bc7aa17c3e2517a52c9fb76dd3239a12c5f03f32052efb040acbb324f0c6a178
Close

Hashes for qsignal-1.0.2b1.tar.gz

Hashes for qsignal-1.0.2b1.tar.gz
AlgorithmHash digest
SHA256603607c0a1ef0b050a5b8806793c1b203c74d34da9815fe87b17a7bf30f8c1ce
MD5a749546330b4da579a18f0fa6fa86d33
BLAKE2-25625f6876034a74406da8b5d40a30e8a9afefd4ceb050bda691e5c4016b4428dac

Build complex application behaviours using signals and slots, and override widget event handling with custom events.

As already described, every interaction the user has with a Qt application causes an Event. There are multiple types of event, each representing a difference type of interaction — e.g. mouse or keyboard events.

Events that occur are passed to the event-specific handler on the widget where the interaction occurred. For example, clicking on a widget will cause a QMouseEvent to be sent to the .mousePressEvent event handler on the widget. This handler can interrogate the event to find out information, such as what triggered the event and where specifically it occurred.

You can intercept events by subclassing and overriding the handler function on the class, as you would for any other function. You can choose to filter, modify, or ignore events, passing them through to the normal handler for the event by calling the parent class function with super().

However, imagine you want to catch an event on 20 different buttons. Subclassing like this now becomes an incredibly tedious way of catching, interpreting and handling these events.

python

Thankfully Qt offers a neater approach to receiving notification of things happening in your application: Signals.

Signals

Instead of intercepting raw events, signals allow you to 'listen' for notifications of specific occurrences within your application. While these can be similar to events — a click on a button — they can also be more nuanced — updated text in a box. Data can also be sent alongside a signal - so as well as being notified of the updated text you can also receive it.

The receivers of signals are called Slots in Qt terminology. A number of standard slots are provided on Qt classes to allow you to wire together different parts of your application. However, you can also use any Python function as a slot, and therefore receive the message yourself.

Load up a fresh copy of `MyApp_window.py` and save it under a new name for this section. The code is copied below if you don't have it yet.

Basic signals

First, let's look at the signals available for our QMainWindow. You can find this information in the Qt documentation. Scroll down to the Signals section to see the signals implemented for this class.

Qt 5 Documentation — QMainWindow Signals

As you can see, alongside the two QMainWindow signals, there are 4 signals inherited from QWidget and 2 signals inherited from Object. If you click through to the QWidget signal documentation you can see a .windowTitleChanged signal implemented here. Next we'll demonstrate that signal within our application.

Qt 5 Documentation — Widget Signals

The code below gives a few examples of using the windowTitleChanged signal.

python

Signal Slot Qt Python Code

Try commenting out the different signals and seeing the effect on what the slot prints.

We start by creating a function that will behave as a ‘slot’ for our signals.

Then we use .connect on the .windowTitleChanged signal. We pass the function that we want to be called with the signal data. In this case the signal sends a string, containing the new window title.

Signal Slot Qt Python Program

If we run that, we see that we receive the notification that the window title has changed.

Events

Qt For Python Signal Slot

Next, let’s take a quick look at events. Thanks to signals, for most purposes you can happily avoid using events in Qt, but it’s important to understand how they work for when they are necessary.

As an example, we're going to intercept the .contextMenuEvent on QMainWindow. This event is fired whenever a context menu is about to be shown, and is passed a single value event of type QContextMenuEvent.

To intercept the event, we simply override the object method with our new method of the same name. So in this case we can create a method on our MainWindow subclass with the name contextMenuEvent and it will receive all events of this type.

If you add the above method to your MainWindow class and run your program you will discover that right-clicking in your window now displays the message in the print statement.

Signal Slot Qt Python Example

Sometimes you may wish to intercept an event, yet still trigger the default (parent) event handler. You can do this by calling the event handler on the parent class using super as normal for Python class methods.

python

This allows you to propagate events up the object hierarchy, handling only those parts of an event handler that you wish.

However, in Qt there is another type of event hierarchy, constructed around the UI relationships. Widgets that are added to a layout, within another widget, may opt to pass their events to their UI parent. In complex widgets with multiple sub-elements this can allow for delegation of event handling to the containing widget for certain events.

However, if you have dealt with an event and do not want it to propagate in this way you can flag this by calling .accept() on the event.

Signal Slot Qt Python Tutorial

Signal slot qt python program

Python Qt Signal Slot

Alternatively, if you do want it to propagate calling .ignore() will achieve this.

Python Qt Signal Slot Example

python
Signal

In this section we've covered signals, slots and events. We've demonstratedsome simple signals, including how to pass less and more data using lambdas.We've created custom signals, and shown how to intercept events, pass onevent handling and use .accept() and .ignore() to hide/show eventsto the UI-parent widget. In the next section we will go on to takea look at two common features of the GUI — toolbars and menus.