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

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

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 7.1k 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.
  • Y Offline
    Y Offline
    Yosemite
    wrote on 8 Feb 2017, 06:05 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
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 8 Feb 2017, 06:30 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
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 8 Feb 2017, 07:31 last edited by mrjj 2 Aug 2017, 08:19
        #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
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 8 Feb 2017, 07:58 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
          • Y Offline
            Y Offline
            Yosemite
            wrote on 9 Feb 2017, 05:12 last edited by
            #5

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

            K 1 Reply Last reply 9 Feb 2017, 11:47
            0
            • Y Yosemite
              9 Feb 2017, 05:12

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

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 9 Feb 2017, 11:47 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

              4/6

              8 Feb 2017, 07:58

              • Login

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