Passing Arguments to the Function
-
i am new to the world of c ++ and qt
I would like to know please why when I add this-> ui-> QLabel
there is an error: no matching function for call to 'run'this is the code :
QFuture<void> future = QtConcurrent::run(hand,
this->folder,
this->cameraAva,
this->CameraImageCaptureAva,
this->CameraImageCaptureArri,
this->cameraArri,
this->ui->label_2) -
@najlou
I presume you mean when you add yourthis->ui->label_2
, which is aQLabel
, you start getting this error.I am not a thread expert, so this is guesswork, but Passing Arguments to the Function
A copy of each argument is made at the point where QtConcurrent::run() is called, and these values are passed to the thread when it begins executing the function
QObject
s are not copyable, so would that lead to the error message?Passing
&this->ui->label_2
instead would be acceptable?However, I don't know whether it's safe passing anything UI to your thread --- you're not allowed to access UI objects in other threads --- so even with pointer is this a safe/sensible thing to do?
-
@jsulm because I will need to use the QLabel, which I can't figure out if I delete for example "this-> cameraAva" or "this-> CameraImageCaptureAva" there is no more this error I think we are limited by a number of variables but is there a solution please ?
error : no matching function for call to 'run' -
@najlou said in Passing Arguments to the Function:
because I will need to use the QLabel
In what way do you need to use it? Do you want to get its text (then simply pass the text instead of whole QLabel), or do you want to change it? If you want to change it then, as already said two times, you are not allowed to do so in other threads than GUI thread!
-
@jsulm ../Rover/rover.cpp:612:45: warning: unused parameter ‘label_2’ [-Wunused-parameter]
QCamera cameraArriere, QLabel label_2
~~^
../Rover/rover.cpp: In member function ‘void Rover::on_Lidar_clicked()’:
../Rover/rover.cpp:734:59: error: no matching function for call to ‘run(void (&)(QString, QCamera, QCameraImageCapture, QCameraImageCapture*, QCamera*, QLabel*), QString&, QCamera*&, QCameraImageCapture*&, QCameraImageCapture*&, QCamera*&, QLabel*&)’
this->ui->label_2);
^
In file included from /usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/QtConcurrent:16,
from ../Rover/rover.cpp:17:
/usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/qtconcurrentrun.h:72:12: note: candidate: ‘template<class T> QFuture<T> QtConcurrent::run(T (*)())’
QFuture<T> run(T (functionPointer)())
^~~
/usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/qtconcurrentrun.h:72:12: note: template argument deduction/substitution failed:
../Rover/rover.cpp:734:59: note: candidate expects 0 arguments, 6 provided
this->ui->label_2);
^
In file included from /usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/QtConcurrent:16,
from ../Rover/rover.cpp:17:
/usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/qtconcurrentrun.h:77:12: note: candidate: ‘template<class T, class Param1, class Arg1> QFuture<T> QtConcurrent::run(T ()(Param1), const Arg1&)’
QFuture<T> run(T (functionPointer)(Param1), const Arg1 &arg1)
^~~
/usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/qtconcurrentrun.h:77:12: note: template argument deduction/substitution failed:
../Rover/rover.cpp:734:59: note: candidate expects 1 argument, 6 provided
this->ui->label_2);
^
In file included from /usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/QtConcurrent:16,
from ../Rover/rover.cpp:17:
/usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/qtconcurrentrun.h:82:12: note: candidate: ‘template<class T, class Param1, class Arg1, class Param2, class Arg2> QFuture<T> QtConcurrent::run(T ()(Param1, Param2), const Arg1&, const Arg2&)’
QFuture<T> run(T (functionPointer)(Param1, Param2), const Arg1 &arg1, const Arg2 &arg2)
^~~
/usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/qtconcurrentrun.h:82:12: note: template argument deduction/substitution failed:
../Rover/rover.cpp:734:59: note: candidate expects 2 arguments, 6 provided
this->ui->label_2);
^
In file included from /usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/QtConcurrent:16,
from ../Rover/rover.cpp:17:
/usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/qtconcurrentrun.h:87:12: note: candidate: ‘template<class T, class Param1, class Arg1, class Param2, class Arg2, class Param3, class Arg3> QFuture<T> QtConcurrent::run(T ()(Param1, Param2, Param3), const Arg1&, const Arg2&, const Arg3&)’
QFuture<T> run(T (functionPointer)(Param1, Param2, Param3), const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3)
^~~
/usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/qtconcurrentrun.h:87:12: note: template argument deduction/substitution failed:
../Rover/rover.cpp:734:59: note: candidate expects 3 arguments, 6 provided
this->ui->label_2);
^
In file included from /usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/QtConcurrent:16,
from ../Rover/rover.cpp:17:
/usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/qtconcurrentrun.h:92:12: note: candidate: ‘template<class T, class Param1, class Arg1, class Param2, class Arg2, class Param3, class Arg3, class Param4, class Arg4> QFuture<T> QtConcurrent::run(T ()(Param1, Param2, Param3, Param4), const Arg1&, const Arg2&, const Arg3&, const Arg4&)’
QFuture<T> run(T (functionPointer)(Param1, Param2, Param3, Param4), const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3, const Arg4 &arg4)
^~~
/usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/qtconcurrentrun.h:92:12: note: template argument deduction/substitution failed:
../Rover/rover.cpp:734:59: note: candidate expects 4 arguments, 6 provided
this->ui->label_2);
^
In file included from /usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/QtConcurrent:16,
from ../Rover/rover.cpp:17:
/usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/qtconcurrentrun.h:97:12: note: candidate: ‘template<class T, class Param1, class Arg1, class Param2, class Arg2, class Param3, class Arg3, class Param4, class Arg4, class Param5, class Arg5> QFuture<T> QtConcurrent::run(T ()(Param1, Param2, Param3, Param4, Param5), const Arg1&, const Arg2&, const Arg3&, const Arg4&, const Arg5&)’
QFuture<T> run(T (*functionPointer)(Param1, Param2, Param3, Param4, Param5), const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3, const Arg4 &arg4, const Arg5 &arg5)
^~~
/usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/qtconcurrentrun.h:97:12: note: template argument deduction/substitution failed:
../Rover/rover.cpp:734:59: note: candidate expects 5 arguments, 6 provided
this->ui->label_2);
^
In file included from /usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/QtConcurrent:16,
from ../Rover/rover.cpp:17:
/usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/qtconcurrentrun.h:105:6: note: candidate: ‘template<class Functor> typename std::enable_if<(! QtPrivate::HasResultType<MapFunctor>::Value), QFuture<decltype (functor())> >::type QtConcurrent::run(Functor)’
auto run(Functor functor) -> typename std::enable_if<!QtPrivate::HasResultType<Functor>::Value, QFuture<decltype(functor())>>::type
^~~
/usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/qtconcurrentrun.h:105:6: note: template argument deduction/substitution failed:
../Rover/rover.cpp:734:59: note: candidate expects 1 argument, 7 provided
this->ui->label_2);
^
In file included from /usr/include/arm-linux-gnueabihf/qt5/QtConcurrent/QtConcurrent:16,
from ../Rover/rover.cpp:17: -
@najlou said in Passing Arguments to the Function:
../Rover/rover.cpp:734:59: note: candidate expects 5 arguments, 6 provided
Yes, it looks like you can't pass more than 5 parameters. If you really need more than 5 parameters you can define a struct where you put all these parameters and then pass this struct as parameter.
-
@jsulm
I note from https://code.woboq.org/qt5/qtbase/src/concurrent/qtconcurrentrun.h.html that there are explicit template definitions going up to 5 parameters. I don't know (can't figure) what happens if you pass 6 or more parameters?@najlou
To prove, try the following. Don't try to run the code, it's just to see whether or not it gets through the compiler!QFuture<void> future = QtConcurrent::run(hand, 1, 2, 3, 4, 5);
That compiles, right?
Now try:QFuture<void> future = QtConcurrent::run(hand, 1, 2, 3, 4, 5, 6, 7);
What about this one?