Qt Signal Slot Pointer Argument

Nov 02, 2009  If the parameter types are incompatible, or if the signal or the slot doesn't exist, Qt will issue a warning at run-time if the application is built in debug mode. Similarly, Qt will give a warning if parameter names are included in the signal or slot signatures. So far, we have only used signals and slots. Signals and slots is a language construct introduced in Qt for communication between objects which makes it easy to implement the observer pattern while avoiding boilerplate code. The concept is that GUI widgets can send signals containing event information which can be received by other widgets / controls using special functions known as slots. This is similar to C/C function pointers, but signal/slot system ensures the type-correctness of callback arguments. With Qt4, you can tell Qt to post signals in the event queue using the 5th argument of the connect-method. 1)Memory leak? Memory leaks occur when pointers referencing a certain chunk of memory goes out of scope and therefore the pointer is lost but the content that the pointer was referencing is still lying in the heap.

Sometimes you want to connect a slot or a functor to a Qt signal and only have it be called once. Adding the necessary logic to enable this can mess up the code unnecessarily, especially with lambdas, since a connection handle is needed to disconnect them.

This tiny framework provides connect functions argument-compatible with the QObject::connect series that will call the slot or functor only once. The logic is simple:

  • Create a pointer that will store the connection handle returned from QObject::connect
  • Capture this pointer in a wrapper function
  • Connect the passed signal to call the wrapper function
  • Disconnect the connection inside the wrapper function and then call the passed functor or slot

Although the fire-once-only logic is simple, additional logic and template code is needed since Once::connect needs to work like QObject::connect: accept all kinds of functors, lambda and slots and conveniently discard superfluous signal arguments. The implementation allows perfect forwarding of arguments and, although it can probably be simplified, should work just as QObject::connect does.

Methods

The following methods are available. Note that the function signature is simplified for readability. Remember that a slot also can be a signal.

How to use

Include once.h in your project and use a compiler that supports C++11 (orhigher). Use Once::connect as you would use QObject::connect. If you needto disconnect the slot/functor/signal, disconnect the returnedQMetaObject::Connection with static bool QObject::disconnect(const QMetaObject::Connection &connection).

In this part of the PyQt5 programming tutorial, we will explore events and signals occurring in applications.

Events

GUI applications are event-driven. Events are generated mainly by the user of an application. But they can be generated by other means as well; e.g. anInternet connection, a window manager, or a timer.When we call the application's exec_() method, the application enters the main loop. The main loop fetches events and sends them to the objects.

In the event model, there are three participants:

  • event source
  • event object
  • event target

The event source is the object whose state changes. It generates events. The event object (event) encapsulates the state changes in the event source.The event target is the object that wants to be notified. Event source object delegates the task of handling an event to the event target.

PyQt5 has a unique signal and slot mechanism to deal with events. Signals and slots are used for communication between objects. A signal is emitted when a particular event occurs. A slot can be any Python callable.A slot is called when its connected signal is emitted.

Signals and slots

This is a simple example demonstrating signals and slots in PyQt5.

sigslot.py

In our example, we display a QtGui.QLCDNumberand a QtGui.QSlider. We change the lcd number by dragging the slider knob.

Here we connect a valueChanged signal of the slider to thedisplay slot of the lcd number.

The sender is an object that sends a signal. The receiver is the object that receives the signal. The slot is the method that reacts to the signal.

Reimplementing event handler

Events in PyQt5 are processed often by reimplementing event handlers.

In our example, we reimplement the keyPressEvent() event handler.

If we click the Escape button, the application terminates.

Event object

Event object is a Python object that contains a number of attributesdescribing the event. Event object is specific to the generated eventtype.

eventobject.py

In this example, we display the x and y coordinates of a mouse pointer in a label widget.

The x and y coordinates are displayd in a QLabelwidget.

Mouse tracking is disabled by default, so the widget only receives mouse move events when at least one mouse button is pressed while the mouse is being moved.If mouse tracking is enabled, the widget receives mouse move events even if no buttons are pressed.

The e is the event object; it contains data about the eventthat was triggered; in our case, a mouse move event. With the x()and y() methods we determine the x and y coordinates of the mouse pointer. We build the string and set it to the label widget.

Event sender

Sometimes it is convenient to know which widget is the sender of a signal. For this, PyQt5 has the sender()method.

We have two buttons in our example. In the buttonClicked() methodwe determine which button we have clicked by calling the sender() method.

Qt Signal Slot Pointer Argument

Both buttons are connected to the same slot.

Signal And Slot In Qt

We determine the signal source by calling the sender() method.In the statusbar of the application, we show the label of the button being pressed.

Emitting signals

Objects created from a QObject can emit signals. The following example shows how we to emit custom signals.

customsignal.py

We create a new signal called closeApp. This signal is emitted during a mouse press event. The signal is connected to theclose() slot of the QMainWindow.

A signal is created with the pyqtSignal() as a class attributeof the external Communicate class.

The custom closeApp signal is connected to the close() slot of the QMainWindow.

Qt Signal Slot Arguments

When we click on the window with a mouse pointer, the closeApp signal is emitted. The application terminates.

In this part of the PyQt5 tutorial, we have covered signals and slots.

Comments are closed.