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::map and class member
QtWS25 Last Chance

QtConcurrent::map and class member

Scheduled Pinned Locked Moved General and Desktop
qtconcurrent
4 Posts 3 Posters 2.6k 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.
  • Y Offline
    Y Offline
    yoavmil
    wrote on 5 May 2015, 06:00 last edited by
    #1

    I want to use QtConcurrent::map on a class member, but it doesn't complie. where as there is a way to use QtConcurrent::run on a class member: QtConcurrent::run(pointer, method, argument ...).
    as a workaround, I used a non-method-function that calls the method function through a static pointer (this will work only on a singlton class, which is my case).
    please advise.

    1 Reply Last reply
    0
    • C Online
      C Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on 5 May 2015, 06:42 last edited by Chris Kawa 5 May 2015, 06:47
      #2

      Assuming MyClass::doIt() is the method you want to call, if you're on a c++11 compiler a simple lambda will do:

      void MyClass::foo() {
          QList<int> list {1,2,3,4};
          auto future = QtConcurrent::map(list, [&](int i){ doIt(i); }); //"this" captured implicitly
          future.waitForFinished();
      }
      

      If not then you can use a lambda-like local struct:

      void MyClass::foo() {
          QList<int> list; //no initializer list in c++98 :(
          list << 1 << 2 << 3 << 4;
      
          struct Lambdish {
              MyClass* m_;
              Lambdish(MyClass* m) :m_(m) {}
              void operator()(int i) { m_->doIt(i); }
          } lambdish(this);
      
          QFuture<void>future = QtConcurrent::map(list, lambdish); //no auto or lambdas in c++98 :(
          future.waitForFinished();
      }
      

      Oh, and just remember that if the parameter is anything but simple type (int, float etc.) the lambda and the lambdish operator() should take it by reference.

      1 Reply Last reply
      0
      • Y Offline
        Y Offline
        yoavmil
        wrote on 5 May 2015, 19:35 last edited by
        #3

        thanks Chris. this is ok. however, I just want to point out the the QtConccurent API is missing the QtConcurrent::map overload that gets a pointer and a method, like QtConcurrent::run has.

        J 1 Reply Last reply 5 May 2015, 23:46
        0
        • Y yoavmil
          5 May 2015, 19:35

          thanks Chris. this is ok. however, I just want to point out the the QtConccurent API is missing the QtConcurrent::map overload that gets a pointer and a method, like QtConcurrent::run has.

          J Offline
          J Offline
          JKSH
          Moderators
          wrote on 5 May 2015, 23:46 last edited by
          #4

          @yoavmil said:

          I just want to point out the the QtConccurent API is missing the QtConcurrent::map overload that gets a pointer and a method, like QtConcurrent::run has.

          Do you mean this?: http://doc.qt.io/qt-5/qtconcurrentmap.html#using-member-functions

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          0

          2/4

          5 May 2015, 06:42

          • Login

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