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
Forum Update on Monday, May 27th 2025

Sample program to pass char array to qtConcurrent

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 678 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 14 Mar 2018, 10:28 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
    • V Offline
      V Offline
      VRonin
      wrote on 14 Mar 2018, 10:55 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

      K 1 Reply Last reply 15 Mar 2018, 08:10
      4
      • V VRonin
        14 Mar 2018, 10:55

        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)));
        
        K Offline
        K Offline
        Kira
        wrote on 15 Mar 2018, 08:10 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

        1/3

        14 Mar 2018, 10:28

        • Login

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