Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How to create a single-shot, one-time connection to a lambda?
-
I use the new Qt5 syntax and lambdas a lot, e. g.:
connect(_textFadeOutAnimation, &QPropertyAnimation::finished, [this, text](){ });
The problem I'm facing right now is I need this slot to only be fired once. Since there's no
ConnectionType
for that, I need todisconnect
inside the slot, apparently. But how do I disconnect a lambda? I can't pass theQMetaObject::Connection
inside the lambda for the obvious reason...
-
Hi @Violet-Giraffe,
Interesting question.
What about something like (untested):
QMetaObject::Connection * const connection = new QMetaObject::Connection; *connection = connect(_textFadeOutAnimation, &QPropertyAnimation::finished, [this, text, connection](){ QObject::disconnect(*connection); delete connection; });
-
@Paul-Colby, brilliant, that works! Thank you. It didn't occur to me
Connection
has an assignment operator.
-
This post is deleted!