QtConcurrent:: run needs static function - why and how to fix it
-
First of all I do not understand why QtConcurrent:: run needs static function.
However, changing the function to static is futile since it creates problem "downstream". By that I mean I cannot implement SIGNAL / SLOTS in static function .HCI_Scan_Dialog *HCI = new HCI_Scan_Dialog (); futureWatcher.setFuture(QtConcurrent::run(HCI->HCI_Scan_Scan));
My function is a method in HCI_Scan_Dialog *HCI = new HCI_Scan_Dialog ()
and using ui gives this error/media/d/QT/QT_PROJECT_CAT/CAT_V1/hci_scan_dialog.cpp:162: error: invalid use of member 'HCI_Scan_Dialog::ui' in static member function
ui->listWidget->addItem(" Found nerby BT devices ");
^I am sure this is simple C /C++ error - not Qt error , however, I still need help to fix it.
If possible to find different way to pass the function / method to the QConcurrent . My guess is it would "fix" the downstream problem and more.
-
Qt 5:
QtConcurrent::run(object, &class::function);
Qt 6:
QtConcurrent::run(&class::function, object);
-
Qt 5:
QtConcurrent::run(object, &class::function);
Qt 6:
QtConcurrent::run(&class::function, object);
@Jonas-Kvinge said in QtConcurrent:: run needs static function - why and how to fix it:
Qt 5:
QtConcurrent::run(object, &class::function);
Qt 6:
QtConcurrent::run(&class::function, object);
OK, the intelisense gives me 84 options !
( I have not found the one I am erroneously using )
Could you treat me as dummy and write down the exact code ?
For Qt5.
I just do not get what is "object" , I can handle the class::function.
I'll try to find an example somewhere. -
@Jonas-Kvinge said in QtConcurrent:: run needs static function - why and how to fix it:
Qt 5:
QtConcurrent::run(object, &class::function);
Qt 6:
QtConcurrent::run(&class::function, object);
OK, the intelisense gives me 84 options !
( I have not found the one I am erroneously using )
Could you treat me as dummy and write down the exact code ?
For Qt5.
I just do not get what is "object" , I can handle the class::function.
I'll try to find an example somewhere.Could you treat me as dummy and write down the exact code ?
For Qt5.QtConcurrent::run(HCI, &HCI_Scan_Dialog::HCI_Scan_Scan);
-
If the HCI_Scan_Scan function takes parameters you need to add those to the end like:
QtConcurrent::run(HCI, &HCI_Scan_Dialog::HCI_Scan_Scan, parameter1, parameter2, etc);
-
If the HCI_Scan_Scan function takes parameters you need to add those to the end like:
QtConcurrent::run(HCI, &HCI_Scan_Dialog::HCI_Scan_Scan, parameter1, parameter2, etc);
@Jonas-Kvinge
Thanks, I got it half right by myself.I did find this , I think I have been there before, but it does not explain why it wanted "static" function.
https://doc.qt.io/qt-5/qtconcurrentrun.html
No , I do not need to pass anything to the function itself.
I actually I should - the function itself builds individual strings and I need to output them to calling dialog.
Perhaps I need to pass a pointer to an array...
As I mentioned elsewhere - this "project" just keeps growing....
Thanks again.
-
@Jonas-Kvinge
Thanks, I got it half right by myself.I did find this , I think I have been there before, but it does not explain why it wanted "static" function.
https://doc.qt.io/qt-5/qtconcurrentrun.html
No , I do not need to pass anything to the function itself.
I actually I should - the function itself builds individual strings and I need to output them to calling dialog.
Perhaps I need to pass a pointer to an array...
As I mentioned elsewhere - this "project" just keeps growing....
Thanks again.
@AnneRanch said in QtConcurrent:: run needs static function - why and how to fix it:
I did find this , I think I have been there before, but it does not explain why it wanted "static" function.
So it is callable. If you pass a pointer-to-member, as you had, then the compiler has no object to call the method on. This is why @Jonas-Kvinge mentioned the relevant overload. Consider the following:
auto method = &HCI_Scan_Dialog::HCI_Scan_Scan; int a = 0; method(a); //< The compiler is: "Huh? What object to call this on?"
In C++ terms the above should be used like this:
auto method = &HCI_Scan_Dialog::HCI_Scan_Scan; int a = 0; HCI_Scan_Dialog dialog; (dialog.*method)(a); //< The compiler is: "Ah, okay, we want to call method on the dialog object"
@AnneRanch said in QtConcurrent:: run needs static function - why and how to fix it:
Perhaps I need to pass a pointer to an array...
Probably not. Rather return the resulting vector from the function, and capture it through the
QFuture
. Something along these lines:typedef QVector<double> MyVector; MyVector functionToRun(); QFuture<MyVector> future = QtConcurent::run(functionToRun); // When the future's ready you can extract with: MyVector result = future.result();
-
@kshegunov said in QtConcurrent:: run needs static function - why and how to fix it:
Probably not. Rather return the resulting vector from the function, and capture it through the QFuture.
The function retrieves data - string - and does not store them.
At present I just use cout to display the data for debug purposes.
How can I return such data without storing in ?Either locally or using external array.
It would be of little use saving it locally.
The data has to be available for further processing.I am not comfortable creating local "vector" array, plain array seems simpler anyway, but willing to try.
Thanks for your comments and help.
-
@kshegunov said in QtConcurrent:: run needs static function - why and how to fix it:
Probably not. Rather return the resulting vector from the function, and capture it through the QFuture.
The function retrieves data - string - and does not store them.
At present I just use cout to display the data for debug purposes.
How can I return such data without storing in ?Either locally or using external array.
It would be of little use saving it locally.
The data has to be available for further processing.I am not comfortable creating local "vector" array, plain array seems simpler anyway, but willing to try.
Thanks for your comments and help.
@AnneRanch said in QtConcurrent:: run needs static function - why and how to fix it:
How can I return such data without storing in ?
You don't. You store it locally and then return it when the function finishes. You can't return any data without allocating storage for it somewhere. In this case, locally is as good a place as any ... actually even better than someplace else (the "why" behind this statement is another kettle of fish altogether).
The data has to be available for further processing.
Yes, that's why you return it from the function, and when the future is ready, you get the data and process it further.
I am not comfortable creating local "vector" array
Why? What's the concern here?
plain array seems simpler anyway, but willing to try.
Did you mean a
QString[]
? If so, then definitely better to useQVector
. For the former you either have to preallocate more memory at the global stack (very much not recommended), or allocate on the heap withnew
and then someone has to clean that up.QVector
already does the latter and wraps it into an easy to use RAII object, which is also optimized to minimize copying the actual data. So I don't see a good reason for your aversion. -
I have successfully implemented QVector<QString>, but cannot do same with QVector<QStringList>
QVector<QString> *string; string->append(" TEST "); QVector<QStringList> *stringVector; stringVector->append("TEST"); this fails - no such method
Getting this error
/media/d/QT/QT_PROJECT_CAT/CAT_V1/hci_scan_dialog.cpp:171: error: no matching function for call to 'QVector<QStringList>::append(const char [5])' stringVector->append("TEST"); ^
Why am I getting an error when intelisense has no problem "suggesting" append method ?
I am not sure why QStringList would add / be better to use then plain QString, but like to at least know why I am getting the error,.
-
stringVector->append(QStringList() << "TEST");
-
I have successfully implemented QVector<QString>, but cannot do same with QVector<QStringList>
QVector<QString> *string; string->append(" TEST "); QVector<QStringList> *stringVector; stringVector->append("TEST"); this fails - no such method
Getting this error
/media/d/QT/QT_PROJECT_CAT/CAT_V1/hci_scan_dialog.cpp:171: error: no matching function for call to 'QVector<QStringList>::append(const char [5])' stringVector->append("TEST"); ^
Why am I getting an error when intelisense has no problem "suggesting" append method ?
I am not sure why QStringList would add / be better to use then plain QString, but like to at least know why I am getting the error,.
@AnneRanch said in QtConcurrent:: run needs static function - why and how to fix it:
like to at least know why I am getting the error
Because "TEST" is not a QStringList...
Intelisense suggests append because there is such a method, so why should it not suggest it? It is your job to pass proper parameter to append.