Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Qt::UniqueConnection works?

I don't quite understand how the Qt::UniqueConnection 'flag' works.

The Qt::UniqueConnection is a constant from the enum Qt::ConnectionType that "describes the types of connection that can be used between signals and slots".

As explained in the documentation, the Qt::UniqueConnection constant is:

Same as AutoConnection, but the connection is made only if it does not duplicate an existing connection. i.e., if the same signal is already connected to the same slot for the same pair of objects, then the connection will fail. This connection type was introduced in Qt 4.6.

Based on the following example:

  for (int index = 0; index < 2; ++index)
  {
      QPushButton *myButton = new QPushButton("Button", this);
      connect(myButton, SIGNAL(clicked()), this, SLOT(doSomething()), Qt::UniqueConnection);
  }
  1. Will it create 2 unique connections (one for each button), based on the button memory address (I suppose)?

  2. Or will it create only one connection (the second one will fail), based on the object type (QPushButton) and the signals and slots used?

like image 817
KelvinS Avatar asked Mar 30 '17 15:03

KelvinS


People also ask

Can we connect one signal with multiple slots?

If several slots are connected to one signal, the slots will be executed one after the other, in the order they have been connected, when the signal is emitted. Signals are automatically generated by the moc and must not be implemented in the . cpp file. They can never have return types (i.e.

What is emit in Qt?

Emit is a Qt macro that will immediately trigger the connected slot with what we passed in a parameter. As we said earlier, removed(Task* this) has no function body, its purpose is to notify the registered slot of an event. Lambdas are a great addition to C++.

What is Queuedconnection in Qt?

Queued Connection The slot is invoked when control returns to the event loop of the receiver's thread. The slot is executed in the receiver's thread. Blocking Queued Connection The slot is invoked as for the Queued Connection, except the current thread blocks until the slot returns.


1 Answers

If you don't put this flag, it means that you can do the same connection: viz. same sender object, same receiver object, same signal and same Slot more than once.

The slot will be called as many number of times as you made this connection.

But you shouldn't abuse of this flag when it is not necessary because it will do a verification to see if your connection already exists, which mean the code will be slower.

Note: Qt::UniqueConnection do not work for lambdas, non-member functions and functors; they only apply to connecting to member functions.

like image 105
basslo Avatar answered Oct 03 '22 03:10

basslo