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. Using QVector<> as std::vector<> without duplicating the data
Forum Update on Monday, May 27th 2025

Using QVector<> as std::vector<> without duplicating the data

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 7.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.
  • YosemiteY Offline
    YosemiteY Offline
    Yosemite
    wrote on last edited by
    #1

    Howdy!

    I'm working with a C++ library that takes a pointer to std::vector<double> and iterates over it, potentially reading millions of values. However, I also plot that data using QCustomPlot, which handles huge data sets quite nicely.

    The lib wants a std::vector<double>, QCustomPlot wants a QVector<double>. Is there a way to keep them both happy without duplicating the data into two vectors? The copy is fast, it just feels like a waste of memory.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Nothing exist like that. If contents are address variable it would have saved something

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      6
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        I assume you are using
        http://doc.qt.io/qt-5/qvector.html#fromStdVector
        ( its still copy. just as in contrast to using a loop)

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          fromStdVector copies the data.

          From a quick look at QCustomPlot (and really just a look), one solution that doesn't seem unreasonable is to modify the sources from QCustomPlot to use std::vector in place of QVector to handle your data.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • YosemiteY Offline
            YosemiteY Offline
            Yosemite
            wrote on last edited by
            #5

            Got it, thanks for these answers. Modifying one library or the other seems like a good solution.

            kshegunovK 1 Reply Last reply
            0
            • YosemiteY Yosemite

              Got it, thanks for these answers. Modifying one library or the other seems like a good solution.

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

              Well, there's also an alternative to modifying the QCustomPlot sources, but it comes with a disclaimer. Consider this specialization:

              class MyType; //< Whatever your type is
              
              template <>
              static inline QVector<MyType> QVector<MyType>::fromStdVector(const std::vector<MyType> & vector)
              {
                  QVector<MyType> tmp;
                  tmp.d = QTypedArrayData<MyType>::fromRawData(vector.data(), vector.size(), Unsharable);
                  return tmp;
              }
              

              Here goes the disclaimer:

              1. I haven't tested that, it's hacking into Qt's internals, so use at your own discretion.
              2. You have to ensure the std::vector instance will not go out of scope while any one QVector instance holds reference to the data

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              2

              • Login

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