Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Const double * to QVector<double>

    General and Desktop
    4
    5
    2731
    Loading More Posts
    • 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.
    • beecksche
      beecksche last edited by beecksche

      Hello,
      i got a pointer to an array with double values. But i need the values in a QVector.

      const double *array = externObject->getArray();
      
      QVector<double> myArray = array;// how to solve this problem
      

      I can do a for loop, but it doesn't look very well:

      QVector<double> myArray;
      for(int i = 0; i < externObject->arrayCount(); i++) {
        myArray << externObject->getArray()[i];
      }
      

      Is there a better way to do that?

      Thanks

      kshegunov raven-worx 2 Replies Last reply Reply Quote 0
      • kshegunov
        kshegunov Moderators @beecksche last edited by

        @beecksche

        int count = externObject->arrayCount();
        const double * array = externObject->getArray();
        
        QVector<double> myArray(count);
        ::memcpy(myArray.data(), array, count * sizeof(double));
        

        Read and abide by the Qt Code of Conduct

        1 Reply Last reply Reply Quote 3
        • VRonin
          VRonin last edited by

          your solution is more than acceptable, an alternative is:

          QVector<double> myArray(externObject->arrayCount());
          std::copy(array,array + externObject->arrayCount(),myArray.begin());
          

          "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

          1 Reply Last reply Reply Quote 2
          • raven-worx
            raven-worx Moderators @beecksche last edited by raven-worx

            @beecksche
            you implicitly already showed that it isn't possible with an assignment operator only. (Edit: but i guess that's not what you were after afterall?)
            In your loop you used arrayCount() to determine the item count. How would you do it in the assignment operator?

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply Reply Quote 0
            • beecksche
              beecksche last edited by

              Thanks everybody for the replies.

              I recognized that i need to modify some values. However the loop seems the best way! Thanks a lot!

              1 Reply Last reply Reply Quote 0
              • First post
                Last post