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. qt5 connect to lambda
Qt 6.11 is out! See what's new in the release blog

qt5 connect to lambda

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 5 Posters 8.0k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    Marek
    wrote on last edited by
    #1

    Hi all,

    Is it possible to connect signal/slot with additional parameters using lambda function and if so, then how?
    I have QNetworkReply::downloadProgress(qint64 recvBytes, qint64 totalFileBytes)
    and I would like to connect this signal to a slot with additional parameter, something like this:
    MyClass::downloadProgress(qint64 recvBytes, qint64 totalFileBytes,quint pointer_to_sender)

    so in other words I need something like this:
    connect(netReply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadProgress(qint64,qint64,netReply)));
    or
    connect(netReply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(downloadProgress(qint64,qint64,my_int_value)));

    How to do it with lambda in qt5 ?

    Best Regards
    Marek

    1 Reply Last reply
    0
    • hskoglundH Offline
      hskoglundH Offline
      hskoglund
      wrote on last edited by
      #2

      Hi the other way around is easy, I mean you can send/Signal more arguments than you want to receive in your Slot (Qt happily throws the extra arguments away for you).

      But receiving more arguments than sending, then Qt has to grab something out of thin air, Qt is good but not that good. :-(

      If you want to send a pointer/identifier back to the sender, you can try something like QSignalMapper.

      M 1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        hi
        Its not possible to use the sender() function for this?
        That will get you the netReply that send the signal.

        M VRoninV 2 Replies Last reply
        3
        • hskoglundH hskoglund

          Hi the other way around is easy, I mean you can send/Signal more arguments than you want to receive in your Slot (Qt happily throws the extra arguments away for you).

          But receiving more arguments than sending, then Qt has to grab something out of thin air, Qt is good but not that good. :-(

          If you want to send a pointer/identifier back to the sender, you can try something like QSignalMapper.

          M Offline
          M Offline
          Marek
          wrote on last edited by
          #4

          @hskoglund
          Thanks for reply,
          but with QSignalMapper I can't grab parameters from signal right? I can set some mappings, but what is being send from signal is lost.

          1 Reply Last reply
          0
          • mrjjM mrjj

            hi
            Its not possible to use the sender() function for this?
            That will get you the netReply that send the signal.

            M Offline
            M Offline
            Marek
            wrote on last edited by
            #5

            @mrjj

            Hi,

            Of course! It works for me ;)
            Thanks for the tip!

            Marek

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              you should use Qt5 connection sintax https://wiki.qt.io/New_Signal_Slot_Syntax.

              connect(netReply,&QNetworkReply::downloadProgress,[=](qint64 bytesReceived, qint64 bytesTotal)->void{downloadProgress(bytesReceived,bytesTotal,netReply);});
              

              or

              connect(netReply,&QNetworkReply::downloadProgress,[&](qint64 bytesReceived, qint64 bytesTotal)->void{downloadProgress(bytesReceived,bytesTotal,my_int_value);});
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              M 1 Reply Last reply
              3
              • mrjjM mrjj

                hi
                Its not possible to use the sender() function for this?
                That will get you the netReply that send the signal.

                VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                @mrjj While useful, isn't sender() a fast way to bad design?

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                mrjjM 1 Reply Last reply
                2
                • VRoninV VRonin

                  @mrjj While useful, isn't sender() a fast way to bad design?

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @VRonin
                  Well if overused it leads to ugly code with many casts.
                  So yes, one could say its not optimal. :)

                  And if possible lambdas, much better indeed.

                  1 Reply Last reply
                  0
                  • VRoninV VRonin

                    you should use Qt5 connection sintax https://wiki.qt.io/New_Signal_Slot_Syntax.

                    connect(netReply,&QNetworkReply::downloadProgress,[=](qint64 bytesReceived, qint64 bytesTotal)->void{downloadProgress(bytesReceived,bytesTotal,netReply);});
                    

                    or

                    connect(netReply,&QNetworkReply::downloadProgress,[&](qint64 bytesReceived, qint64 bytesTotal)->void{downloadProgress(bytesReceived,bytesTotal,my_int_value);});
                    
                    M Offline
                    M Offline
                    Marek
                    wrote on last edited by Marek
                    #9

                    @VRonin

                    It would be what I was asking for, but I have an error during compilation

                    download-mgr.cpp:29:44: error: invalid use of non-static member function 'void QNetworkReply::downloadProgress(qint64, qint64)'
                             connect(d->netReply,QNetworkReply::downloadProgress,[=](qint64 bytesReceived, qint64 bytesTotal)->void{downloadProgress(bytesReceived,bytesTotal,d->netReply);});
                    

                    My header:

                    void downloadProgress(qint64 recvBytes,qint64 totalFileBytes,QNetworkReply *netReply);
                    

                    source:

                    void DownloadMgr::downloadProgress(qint64 recvBytes, qint64 totalFileBytes,QNetworkReply *netReply) {
                        downloadMap[netReply]->recvBytes=recvBytes;
                        this->calcPercentDownloaded();
                    }
                    

                    connect:

                    connect(d->netReply,QNetworkReply::downloadProgress,[=](qint64 bytesReceived, qint64 bytesTotal)->void{downloadProgress(bytesReceived,bytesTotal,d->netReply);});
                    
                    

                    What can be wrong?

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by VRonin
                      #10

                      sorry forgot an &

                      connect(d->netReply,&QNetworkReply::downloadProgress etcetera

                      I will correct the post above accordigly

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      2
                      • mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #11

                        Just a note
                        if using older mingw
                        you might need
                        CONFIG+=C++11 ( in the .pro file)
                        to enable c++11 which this syntax needs.

                        1 Reply Last reply
                        2
                        • M Offline
                          M Offline
                          Marek
                          wrote on last edited by
                          #12

                          Thanks all,

                          It works as it should,
                          Thanks a lot ;)
                          Very useful this new connect with lambda

                          Marek

                          1 Reply Last reply
                          1
                          • sonichyS Offline
                            sonichyS Offline
                            sonichy
                            wrote on last edited by
                            #13
                            connect(reply, &QNetworkReply::downloadProgress, [=](qint64 bytesReceived, qint64 bytesTotal){
                            ......
                            });
                            

                            https://github.com/sonichy

                            1 Reply Last reply
                            2

                            • Login

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