Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved How to create a single-shot, one-time connection to a lambda?

    General and Desktop
    6
    7
    8003
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • V
      Violet Giraffe last edited by

      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 to disconnect inside the slot, apparently. But how do I disconnect a lambda? I can't pass the QMetaObject::Connection inside the lambda for the obvious reason...

      beecksche 1 Reply Last reply Reply Quote 0
      • Paul Colby
        Paul Colby last edited by

        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;
        });
        
        V 1 Reply Last reply Reply Quote 7
        • V
          Violet Giraffe @Paul Colby last edited by Violet Giraffe

          @Paul-Colby, brilliant, that works! Thank you. It didn't occur to me Connection has an assignment operator.

          qwasder85 1 Reply Last reply Reply Quote 0
          • beecksche
            beecksche @Violet Giraffe last edited by beecksche

            This post is deleted!
            1 Reply Last reply Reply Quote 0
            • S
              seyed last edited by

              There is another way. Create a context object and use it as receiver for the connection. If you delete it, the connected signal will be disconnected.

              QObject* ctx = new QObject();
              connect(d, &MyClass::mySignal, ctx, [=]() {
                  // destroy the context/receiver to disconnect
                  ctx->deleteLater();
              
                  // slot routine here...
              });
              
              1 Reply Last reply Reply Quote 1
              • qwasder85
                qwasder85 @Violet Giraffe last edited by

                @Violet-Giraffe Mark the thread as 'solved', please.

                1 Reply Last reply Reply Quote 0
                • T
                  thomaso last edited by

                  In Qt6 there is a new connection type Qt::SingleShotConnection.
                  See more in this KDAB blog: https://www.kdab.com/single-shot-connections/

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post