Important: Please read the Qt Code of Conduct -

qt5 connect to lambda

  • 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:

    How to do it with lambda in qt5 ?

    Best Regards

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

  • Lifetime Qt Champion

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

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

  • @mrjj


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


  • you should use Qt5 connection sintax

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


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

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

  • Lifetime Qt Champion

    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.

  • @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);


    void DownloadMgr::downloadProgress(qint64 recvBytes, qint64 totalFileBytes,QNetworkReply *netReply) {


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

    What can be wrong?

  • sorry forgot an &

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

    I will correct the post above accordigly

  • Lifetime Qt Champion

    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.

  • Thanks all,

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


  • connect(reply, &QNetworkReply::downloadProgress, [=](qint64 bytesReceived, qint64 bytesTotal){

Log in to reply