Unsolved Saving a vector of objects and clases to a file.
-
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,
-
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.
-
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.
-
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.
-
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 :) -
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. -
@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. :) -
@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 useQMetaType::create
.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.