[Interest] Using connect/disconnect with lambdas

Pierre-Yves Siret py.siret at gmail.com
Mon Feb 19 09:46:09 CET 2018


2018-02-19 5:59 GMT+01:00 Tom Isaacson via Interest <interest at qt-project.org
>:

> 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(); });
>

You can use connect(m_pRouteEditNameAct, &tAction::triggered,
this, QOverload<>::of(&YourClass::RouteEditName))

and QOverload<QString>::of(&YourClass::RouteEditName) for the other
overload.

More info here: http://doc.qt.io/qt-5/qtglobal.html#qOverload (note that
the c++14 syntax is shorter than the one I described).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20180219/3f492119/attachment.html>


More information about the Interest mailing list