[Interest] Using connect/disconnect with lambdas

Tom Isaacson Tom.Isaacson at navico.com
Mon Feb 19 05:59:50 CET 2018


I'm replacing some old SIGNAL/SLOT connects with the new Qt5 format and I need to use lambdas for some of them.

Scenario 1:

public slots:
    void RouteEditName();
    void RouteEditName(QString name);

    m_pRouteEditNameAct = new tAction(tr("Name route") + "...", this);
    connect(m_pRouteEditNameAct, SIGNAL(triggered()), this, SLOT(RouteEditName()), Qt::QueuedConnection);

If someone has been naughty and overloaded a slot then normally I'd just use a lambda to indicate which slot is being called:
    connect(m_pRouteEditNameAct, this, &tAction::triggered, [this]() { RouteEditName(); });

But in this case I need to add Qt::QueuedConnection but connect() won't take four params like this:
    connect(m_pRouteEditNameAct, this, &tAction::triggered, [this]() { RouteEditName(); }, Qt::QueuedConnection);

Do I just add an unnecessary 'this' to fill it out?
    connect(m_pRouteEditNameAct, this, &tAction::triggered, this, [this]() { RouteEditName(); }, Qt::QueuedConnection);


Scenario 2:

Later I need to disconnect this:
    disconnect(m_pRouteEditNameAct, SIGNAL(triggered()), this, SLOT(RouteEditName()));

But how do I disconnect a lambda?
    disconnect(m_pRouteEditNameAct, &tAction::triggered, this, [this]() { RouteEditName(); });

This seems like it's trying to disconnect from a different lambda to the one I originally connected to.

Thanks.

Tom Isaacson




More information about the Interest mailing list