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. Const double * to QVector<double>
Forum Update on Monday, May 27th 2025

Const double * to QVector<double>

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 3.2k 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.
  • beeckscheB Offline
    beeckscheB Offline
    beecksche
    wrote on last edited by beecksche
    #1

    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

    kshegunovK raven-worxR 2 Replies Last reply
    0
    • beeckscheB 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

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @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
      3
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        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
        2
        • beeckscheB 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

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @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
          0
          • beeckscheB Offline
            beeckscheB Offline
            beecksche
            wrote on last edited by
            #5

            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
            0

            • Login

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