Qt Signal Slot Connection Type

Signals are connected to slots using the connect method of a bound signal. Connect (slot , type=PyQt5.QtCore.Qt.AutoConnection , noreceivercheck=False) → PyQt5.QtCore.QMetaObject.Connection¶ Connect a signal to a slot. An exception will be raised if the connection failed.

Qt Signal Slot Example

Signal

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.

Signal And Slot

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.

Qt signal slot example

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

Signal And Slot In Qt

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).