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 Tree model to QDataStream
QtWS25 Last Chance

How to write Tree model to QDataStream

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 352 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.
  • S Offline
    S Offline
    summit
    wrote on last edited by
    #1

    I have a QAbstractModel Tree structure i want to write all of my tree Items to the stream and load them back in the same hierarchy.

    Please help me with any references or code that i can refer to .

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

      Hi,

      Isn't that mostly the same question as here ?

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

      S 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Isn't that mostly the same question as here ?

        S Offline
        S Offline
        summit
        wrote on last edited by
        #3

        @SGaist this is more specific towards QDataStream.

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

          Hi
          Well the first step is to implement
          QDataStream &operator<<(QDataStream &, const QXxx &);
          QDataStream &operator>>(QDataStream &, QXxx &);
          for all classes involved.
          Then you need to traverse the tree and output
          each parent and its children. I think QList can be directly streamed if the class it holds has << >> overrides.

          Most Qt types can be directly streamed so often the operator override is quite trivial and
          only needs you to stream its class members.

          please see
          https://doc.qt.io/qt-5/qdatastream.html
          and section
          Reading and Writing Other Qt Classe

          Also please note the easy to make mistake.
          The operators << and >> takes reference to the type.
          so when you stream a pointer,
          you need to dereference it or it will just think "hey - its an int" and save the address.
          so
          stream << *someptr;
          and not just plain
          stream << someptr;
          as that will just save its adress.

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

            Hi
            There is also
            https://github.com/VSRonin/Qt-Model-Serialisation
            which might be useful if using a QAbstractItemModel model.

            S 1 Reply Last reply
            4
            • mrjjM mrjj

              Hi
              There is also
              https://github.com/VSRonin/Qt-Model-Serialisation
              which might be useful if using a QAbstractItemModel model.

              S Offline
              S Offline
              summit
              wrote on last edited by
              #6

              @mrjj Thank you very much this is indeed very helpful

              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