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. Sample program to pass char array to qtConcurrent

Sample program to pass char array to qtConcurrent

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 747 Views 1 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.
  • KiraK Offline
    KiraK Offline
    Kira
    wrote on last edited by
    #1

    Hello Everyone,
    Looking for a sample program to pass char array to QTConcurrent.
    I have a function which takes char array as an argument and need it pass to the
    QtConcurrent.run() function.
    I tried various ways of doing the same but no success.

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      The problem is not passing it, it's keeping it alive until run finishes. Can you use QByteArray or std::array instead of the raw array?

      EDIT:

      This is a bit awkward but it should work:

      // void doSomething(char* arr, int n)
      char myarray[100];
      std::fill(std::begin(myarray),std::end(myarray),'A');
      ///////////////////////////////////////////////////////////////
      const int arraySize = std::extent<decltype(myarray)>::value;
      char* arrayToPass = new char[arraySize];
      std::memcpy(arrayToPass,myarray,arraySize);
      auto runWatcher =new QFutureWatcher<void>();
      QObject::connect(runWatcher,&QFutureWatcher::finished,runWatcher,&QFutureWatcher::deleteLater);
      QObject::connect(runWatcher,&QFutureWatcher::finished,[arrayToPass]()->void{delete [] arrayToPass;});
      runWatcher->setFuture(QtConcurrent::run(std::bind(doSomething,arrayToPass,arraySize)));
      

      "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

      KiraK 1 Reply Last reply
      4
      • VRoninV VRonin

        The problem is not passing it, it's keeping it alive until run finishes. Can you use QByteArray or std::array instead of the raw array?

        EDIT:

        This is a bit awkward but it should work:

        // void doSomething(char* arr, int n)
        char myarray[100];
        std::fill(std::begin(myarray),std::end(myarray),'A');
        ///////////////////////////////////////////////////////////////
        const int arraySize = std::extent<decltype(myarray)>::value;
        char* arrayToPass = new char[arraySize];
        std::memcpy(arrayToPass,myarray,arraySize);
        auto runWatcher =new QFutureWatcher<void>();
        QObject::connect(runWatcher,&QFutureWatcher::finished,runWatcher,&QFutureWatcher::deleteLater);
        QObject::connect(runWatcher,&QFutureWatcher::finished,[arrayToPass]()->void{delete [] arrayToPass;});
        runWatcher->setFuture(QtConcurrent::run(std::bind(doSomething,arrayToPass,arraySize)));
        
        KiraK Offline
        KiraK Offline
        Kira
        wrote on last edited by
        #3

        @VRonin : Thanks a lot. The implementation looks quite complex so moving with string instead of char array as you have mentioned in previous thread.

        1 Reply Last reply
        0

        • Login

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