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. How to write entire QVector to a binary file?
Forum Updated to NodeBB v4.3 + New Features

How to write entire QVector to a binary file?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qvectorbinary formatqdatastream
46 Posts 8 Posters 14.0k Views 2 Watching
  • 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.
  • J J.Hilk
    18 Nov 2020, 06:20

    @CJha said in How to write entire QVector to a binary file?:

    I do not use const QVector<double>& because I want to depend on Qt's implicit sharing i.e. if the QVector<double> is changed while I am still using the previous QVector to write data in my binary file then it would not affect my "Writer" thread

    So that you know, when passing your QVector through Qt::QueuedConnection - which is the default and correct one across threads - your QVector will be copied auto automatically, there will be no share until write. There will be a copy inside your thread

    C Offline
    C Offline
    CJha
    wrote on 18 Nov 2020, 08:25 last edited by
    #41

    @J-Hilk Thanks, one thing which I am not sure about: What would happen if I use const QVector<double>& instead of QVector<double>, will it still copy the data if the connection type is Qt:QueuedConnection or will it just copy the reference for the vector?

    K 1 Reply Last reply 18 Nov 2020, 08:33
    0
    • C CJha
      18 Nov 2020, 08:25

      @J-Hilk Thanks, one thing which I am not sure about: What would happen if I use const QVector<double>& instead of QVector<double>, will it still copy the data if the connection type is Qt:QueuedConnection or will it just copy the reference for the vector?

      K Offline
      K Offline
      KroMignon
      wrote on 18 Nov 2020, 08:33 last edited by KroMignon
      #42

      @CJha said in How to write entire QVector to a binary file?:

      will it still copy the data if the connection type is Qt:QueuedConnection or will it just copy the reference for the vector?

      In short: Yes they will be copied.
      For more details take a look at this => https://www.embeddeduse.com/2013/06/29/copied-or-not-copied-arguments-signals-slots/

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      C 1 Reply Last reply 18 Nov 2020, 08:35
      3
      • K KroMignon
        18 Nov 2020, 08:33

        @CJha said in How to write entire QVector to a binary file?:

        will it still copy the data if the connection type is Qt:QueuedConnection or will it just copy the reference for the vector?

        In short: Yes they will be copied.
        For more details take a look at this => https://www.embeddeduse.com/2013/06/29/copied-or-not-copied-arguments-signals-slots/

        C Offline
        C Offline
        CJha
        wrote on 18 Nov 2020, 08:35 last edited by
        #43

        @KroMignon Thanks :)

        J 1 Reply Last reply 18 Nov 2020, 09:29
        0
        • C CJha
          18 Nov 2020, 08:35

          @KroMignon Thanks :)

          J Online
          J Online
          JonB
          wrote on 18 Nov 2020, 09:29 last edited by
          #44

          @CJha
          According to @KroMignon's link

          The conclusion from the above results is that we should pass arguments to signals and slots by const reference and not by value. This advice is true for both direct and queued connections. Even if the sender of the signal and the receiver of the slot are in different threads, we should still pass arguments by const reference.

          If you have both signal & slot declared with QVector<double> vec and you change both to have const QVector<double>&, you reduce 3 copies to 1.

          Having said that: I do get lost as to what gets copied, aren't we only talking about the QVector structure and not the data it references??

          C 1 Reply Last reply 18 Nov 2020, 13:05
          0
          • J JonB
            18 Nov 2020, 09:29

            @CJha
            According to @KroMignon's link

            The conclusion from the above results is that we should pass arguments to signals and slots by const reference and not by value. This advice is true for both direct and queued connections. Even if the sender of the signal and the receiver of the slot are in different threads, we should still pass arguments by const reference.

            If you have both signal & slot declared with QVector<double> vec and you change both to have const QVector<double>&, you reduce 3 copies to 1.

            Having said that: I do get lost as to what gets copied, aren't we only talking about the QVector structure and not the data it references??

            C Offline
            C Offline
            CJha
            wrote on 18 Nov 2020, 13:05 last edited by
            #45

            @JonB If it's a copy then I assume it is always going to be the data because the structure is not that significant in terms of memory usage, but I am not sure about it either.

            J 1 Reply Last reply 27 Nov 2020, 14:34
            0
            • C CJha
              18 Nov 2020, 13:05

              @JonB If it's a copy then I assume it is always going to be the data because the structure is not that significant in terms of memory usage, but I am not sure about it either.

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 27 Nov 2020, 14:34 last edited by
              #46

              @CJha said in How to write entire QVector to a binary file?:

              If it's a copy then I assume it is always going to be the data

              No, because Qt containers use copy-on-write. See https://doc.qt.io/qt-5/implicit-sharing.html

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              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