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. Problem in passing function to QtConcurrent

Problem in passing function to QtConcurrent

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.8k Views
  • 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.
  • K Offline
    K Offline
    Kira
    wrote on 13 Mar 2018, 09:35 last edited by VRonin
    #1

    I have declared an structure as:
    struct SaveImage{ char fileName[100]; Mat image; };
    and then qqueue of as: QQueue<SaveImage> saveQueue;

    In-one function i am declaring the object of the queue and passing the value to the queue and calling the qtconcurrent function as:

    fun A()
    {
         SaveImage str;  //Object of the queue
                 str = saveQueue.takeFirst();
               future = QtConcurrent::run(this,&MainWindow::saveOpeartion, &str.fileName, str.image);
    }
    
    
    // Given below is the save operation function:
    //void MainWindow::saveOperation(char fileName[], Mat capturedFrame)
    {
    }
    

    I am getting the following error:

    'QtConcurrent::VoidStoredMemberFunctionPointerCall2<T,Class,Param1,Arg1,Param2,Arg2>::arg1': array initialization requires a brace-enclosed initializer list
    with
    [
        T=void,
        Class=MainWindow,
        Param1=char [],
        Arg1=char [100],
        Param2=cv::Mat,
        Arg2=cv::Mat
    ]
    

    Just need to know the approach of declaring making qqueue of struct is correct or not or how we can achieve the above mentioned implementation.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 13 Mar 2018, 09:54 last edited by VRonin
      #2
      1. Why, in 2018, in C++, would you use char[] to hold a string? Use QString or std::string
      2. QtConcurrent on a member function is VERY dangerous. You have to make sure there are no race conditions manually (including making sure the object doesn't get deleted before run finishes).
      3. You can still do what you want using QtConcurrent::run(std::bind(&MainWindow::saveOpeartion,this,str.fileName,str.image)); but, if you don't use a proper string class as I suggested above, you have to find a way to keep str alive until run() finishes
      4. You are passing Mat around by value. If it's not implicitly shared (or you can force some std::move magic) that might be fairly expensive in terms of resources

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      K 1 Reply Last reply 13 Mar 2018, 10:24
      2
      • V VRonin
        13 Mar 2018, 09:54
        1. Why, in 2018, in C++, would you use char[] to hold a string? Use QString or std::string
        2. QtConcurrent on a member function is VERY dangerous. You have to make sure there are no race conditions manually (including making sure the object doesn't get deleted before run finishes).
        3. You can still do what you want using QtConcurrent::run(std::bind(&MainWindow::saveOpeartion,this,str.fileName,str.image)); but, if you don't use a proper string class as I suggested above, you have to find a way to keep str alive until run() finishes
        4. You are passing Mat around by value. If it's not implicitly shared (or you can force some std::move magic) that might be fairly expensive in terms of resources
        K Offline
        K Offline
        Kira
        wrote on 13 Mar 2018, 10:24 last edited by
        #3

        @VRonin : Can you please elaborate point 4

        V 1 Reply Last reply 13 Mar 2018, 10:34
        0
        • K Kira
          13 Mar 2018, 10:24

          @VRonin : Can you please elaborate point 4

          V Offline
          V Offline
          VRonin
          wrote on 13 Mar 2018, 10:34 last edited by VRonin
          #4

          @Kira said in Problem in passing function to QtConcurrent:

          @VRonin : Can you please elaborate point 4

          I mean, every time you call saveOperation you are triggering a copy of Mat. Since I assume that's an image, if it does not have cheap copy constructors like Qt does it might take a long time to do the copy

          EDIT:

          I just noticed Mat is cv::Mat. The copy constructor is cheap but you have to remember that, from https://docs.opencv.org/trunk/d3/d63/classcv_1_1Mat.html#a294eaf8a95d2f9c7be19ff594d06278e:

          when you modify the matrix formed using such a constructor, you also modify the corresponding elements of m

          So you are not really copying it at all

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          K 1 Reply Last reply 15 Mar 2018, 08:08
          3
          • V VRonin
            13 Mar 2018, 10:34

            @Kira said in Problem in passing function to QtConcurrent:

            @VRonin : Can you please elaborate point 4

            I mean, every time you call saveOperation you are triggering a copy of Mat. Since I assume that's an image, if it does not have cheap copy constructors like Qt does it might take a long time to do the copy

            EDIT:

            I just noticed Mat is cv::Mat. The copy constructor is cheap but you have to remember that, from https://docs.opencv.org/trunk/d3/d63/classcv_1_1Mat.html#a294eaf8a95d2f9c7be19ff594d06278e:

            when you modify the matrix formed using such a constructor, you also modify the corresponding elements of m

            So you are not really copying it at all

            K Offline
            K Offline
            Kira
            wrote on 15 Mar 2018, 08:08 last edited by
            #5

            @VRonin : thanks have implemented the above logic using string instead of character arrary
            Will also keep in mind the instructions for cv::Mat

            1 Reply Last reply
            0

            1/5

            13 Mar 2018, 09:35

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved