Solved 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:
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 -
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.
-
hi
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. -
-
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);});
-
@mrjj While useful, isn't
sender()
a fast way to bad design? -
@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.
-
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?
-
sorry forgot an
&
connect(d->netReply,&QNetworkReply::downloadProgress
etceteraI will correct the post above accordigly
-
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 lambdaMarek
-
connect(reply, &QNetworkReply::downloadProgress, [=](qint64 bytesReceived, qint64 bytesTotal){ ...... });