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. Saving a vector of objects and clases to a file.
Forum Updated to NodeBB v4.3 + New Features

Saving a vector of objects and clases to a file.

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 2.0k Views 3 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.
  • Dan3460D Offline
    Dan3460D Offline
    Dan3460
    wrote on last edited by
    #1

    I have a vector of objects called machines. Each machine in the vector have severa parameters that are used on the program. I also have a couple of more classes that contain program parameters. I need to save all this information to disk so I can run different scenarios of this program.
    I created a overload operator for saving the files, but as I'm adding parameters es becoming very cumbersome to keep up with the writing and reading of the file.
    Is there a way to simplify this task? I remember a couple of years ago working on Visual c++ that I could send the a whole class to disk and retrieve it in the same manner. memory not good was a couple of years ago so I may be lying.

    Thanks for your help,

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

      Hi,

      One way to do it is to implement the QDataStream/QTextStream operators for your class. Then the "dumping" of the vector containing your object should be done automatically for you. If you change your class you'll have to update your operators as well.

      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
      0
      • Dan3460D Offline
        Dan3460D Offline
        Dan3460
        wrote on last edited by
        #3

        Yes, this is the way that I have it implemented, but the machine object is getting up there in the number of parameters. I was looking for something else.

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

          Is your machine a QObject ? If so you can make your parameters Q_PROPERTIES and use QMetaObject's introspections capabilities to automate the loading and saving part.

          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
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            Is save and loading scenarios as core feature that end user also will have?

            If yes, I recommend that you spend a little time considering adding some
            version info to the files to be able to handle loading old scenario file into
            new program as there will be mismatch between real objects and saved data.
            with at least 1 version stamp u can detect the situation :)

            1 Reply Last reply
            1
            • Dan3460D Offline
              Dan3460D Offline
              Dan3460
              wrote on last edited by
              #6

              No is not a qojbect, not very versed on its use.
              Yes, been able to save a few versions of the program is necessary to test different setups.
              I have thought about the handling older files.

              mrjjM 1 Reply Last reply
              1
              • Dan3460D Dan3460

                No is not a qojbect, not very versed on its use.
                Yes, been able to save a few versions of the program is necessary to test different setups.
                I have thought about the handling older files.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @Dan3460
                Ok, using QObject as base class means your class can be part of the Qt meta info system and let you ask about properties (member vars) and other cool stuff which can make
                serialisation more smooth.
                you would just add QObject as base class and add Q_OBJECT macro to class definition.
                It does change one thing though. QObjects are not clonable. so your child would have that constrain too. :)

                kshegunovK 1 Reply Last reply
                0
                • mrjjM mrjj

                  @Dan3460
                  Ok, using QObject as base class means your class can be part of the Qt meta info system and let you ask about properties (member vars) and other cool stuff which can make
                  serialisation more smooth.
                  you would just add QObject as base class and add Q_OBJECT macro to class definition.
                  It does change one thing though. QObjects are not clonable. so your child would have that constrain too. :)

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

                  @mrjj said:

                  you would just add QObject as base class and add Q_OBJECT macro to class definition.
                  It does change one thing though. QObjects are not clonable. so your child would have that constrain too. :)

                  Not a good idea in principle, exactly because QObject is not clonable and there's no (straightforward) way to use QMetaType::create.

                  @Dan3460

                  Register (or at least declare) your classes as meta-types and save their metatype id first, then request them to write their data, that's for serialization. As for the other way around - read the metatype id, create the object and request it reads its data.
                  For example usage look here: Message class declaration, definition, serialization, deserialization.

                  Or create your own factory, which does basically the same thing, but might be a bit cleaner code-wise.

                  Kind regards.

                  Read and abide by the 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