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. QtConcurrent:: run needs static function - why and how to fix it
Forum Updated to NodeBB v4.3 + New Features

QtConcurrent:: run needs static function - why and how to fix it

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 1.8k Views 3 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Jonas KvingeJ Offline
      Jonas KvingeJ Offline
      Jonas Kvinge
      wrote on last edited by
      #2

      Qt 5:

      QtConcurrent::run(object, &class::function);
      

      Qt 6:

      QtConcurrent::run(&class::function, object);
      
      A 1 Reply Last reply
      6
      • Jonas KvingeJ Jonas Kvinge

        Qt 5:

        QtConcurrent::run(object, &class::function);
        

        Qt 6:

        QtConcurrent::run(&class::function, object);
        
        A Offline
        A Offline
        Anonymous_Banned275
        wrote on last edited by
        #3

        @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 KvingeJ 1 Reply Last reply
        0
        • A Anonymous_Banned275

          @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 KvingeJ Offline
          Jonas KvingeJ Offline
          Jonas Kvinge
          wrote on last edited by
          #4

          Could you treat me as dummy and write down the exact code ?
          For Qt5.

          QtConcurrent::run(HCI, &HCI_Scan_Dialog::HCI_Scan_Scan);
          
          1 Reply Last reply
          1
          • Jonas KvingeJ Offline
            Jonas KvingeJ Offline
            Jonas Kvinge
            wrote on last edited by
            #5

            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);
            
            A 1 Reply Last reply
            4
            • Jonas KvingeJ Jonas Kvinge

              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);
              
              A Offline
              A Offline
              Anonymous_Banned275
              wrote on last edited by
              #6

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

              kshegunovK 1 Reply Last reply
              1
              • A Anonymous_Banned275

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

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

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

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                6
                • A Offline
                  A Offline
                  Anonymous_Banned275
                  wrote on last edited by
                  #8

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

                  kshegunovK 1 Reply Last reply
                  0
                  • A Anonymous_Banned275

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

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by kshegunov
                    #9

                    @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 use QVector. For the former you either have to preallocate more memory at the global stack (very much not recommended), or allocate on the heap with new 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.

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    3
                    • A Offline
                      A Offline
                      Anonymous_Banned275
                      wrote on last edited by
                      #10

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

                      jsulmJ 1 Reply Last reply
                      0
                      • Jonas KvingeJ Offline
                        Jonas KvingeJ Offline
                        Jonas Kvinge
                        wrote on last edited by
                        #11
                        stringVector->append(QStringList() << "TEST");
                        
                        1 Reply Last reply
                        2
                        • A Anonymous_Banned275

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

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

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

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          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