Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Qt::ConnectionType to QObject::connect when connecting a lambda?

I am connecting lambdas to QObject's signals:

    QObject::connect(handle, &BatchHandle::progressMax, [this](const ProcessHandle* const self, const int value) {
        this->maxProgress(value);
    });

The code above compiles with no problems.

However it's absolutely necessary that the Qt::QueuedConnection because the handle object will eventually move to another thread.

I added this to my code:

    QObject::connect(handle, &BatchHandle::finished, [this](const ProcessHandle* const self) {
        this->processIsRunning(false);
    }, (Qt::ConnectionType)Qt::QueuedConnection);

Notice how I added explicit cast to make sure it identifies the value type correctly. Result:

1>src\TechAdminServices\database\techCore\processes\import\ImportManagerDialog.cpp(191): error C2664: 'QMetaObject::Connection QObject::connect<void(__cdecl taservices::ProcessHandle::* )(const taservices::ProcessHandle *),Qt::ConnectionType>(const taservices::ProcessHandle *,Func1,const QObject *,Func2,Qt::ConnectionType)' : cannot convert parameter 3 from 'taservices::`anonymous-namespace'::<lambda58>' to 'const QObject *'
1>          with
1>          [
1>              Func1=void (__cdecl taservices::ProcessHandle::* )(const taservices::ProcessHandle *),
1>              Func2=Qt::ConnectionType
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

How to get a queued connection when connecting a lambda?

like image 658
Tomáš Zato - Reinstate Monica Avatar asked Feb 10 '17 14:02

Tomáš Zato - Reinstate Monica


People also ask

How do I connect Qt signals and slots?

To connect the signal to the slot, we use QObject::connect(). There are several ways to connect signal and slots. The first is to use function pointers: connect(sender, &QObject::destroyed, this, &MyObject::objectDestroyed);

Can we connect one signal with multiple slots?

You can connect as many signals as you want to a single slot, and a signal can be connected to as many slots as you desire. It is even possible to connect a signal directly to another signal. (This will emit the second signal immediately whenever the first is emitted.)

How do you emit signal in Qt?

In Qt, we have an alternative to the callback technique: We use signals and slots. A signal is emitted when a particular event occurs. Qt's widgets have many predefined signals, but we can always subclass widgets to add our own signals to them. A slot is a function that is called in response to a particular signal.

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

I think you need to use the QObject::connect overload that allows you to specify the context in which the lambda should be invoked...

QObject::connect(
  handle,
  &BatchHandle::progressMax,
  target_context,   /* Target context parameter. */
  [this](const ProcessHandle* const self, const int value)
  {
    this->maxProgress(value);
  },
  Qt::QueuedConnection);
like image 94
G.M. Avatar answered Sep 22 '22 17:09

G.M.