Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to create a single-shot, one-time connection to a lambda?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 6 Posters 11.6k Views
  • 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 Offline
    V Offline
    Violet Giraffe
    wrote on last edited by
    #1

    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...

    beeckscheB 1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      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
      7
      • Paul ColbyP Paul Colby

        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 Offline
        V Offline
        Violet Giraffe
        wrote on last edited by Violet Giraffe
        #3

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

        qwasder85Q 1 Reply Last reply
        0
        • V Violet Giraffe

          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...

          beeckscheB Offline
          beeckscheB Offline
          beecksche
          wrote on last edited by beecksche
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • S Offline
            S Offline
            seyed
            wrote on last edited by
            #5

            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
            1
            • V Violet Giraffe

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

              qwasder85Q Offline
              qwasder85Q Offline
              qwasder85
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • T Offline
                T Offline
                thomaso
                wrote on last edited by
                #7

                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
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved