Custom property for QDataStream?
-
Hi, I have a data class and I use
QDataStream
to read and write it to the storage. I would like to pass an additional property to theQDataStream
class, i.e. set something similar tobool QObject::setProperty(const char *name, const QVariant &value)
but on theQDataStream
object before using it to read/write my data class. I do not want to make this property a member of my data class since it has no use except when reading and writing from storage. Is this possible? I have looked up theQDataStream
andQIODeviceBase
class documentation and I could not find any way to add a property toQDataStream
or to pass additional arguments when using the operatorsQDataStream& operator<<
andQDataStream& operator>>
. -
@CJha
Do you mean you want this "additional property" to be output when serialising (i.e. it ends up in the actual data streamed), so that e.g. it will be read back upon deserialising? In that case you should be able to include that in your data class's<<
/>>
operators? -
@JonB No, this additional property is not supposed to be written to the data file when serializing. It only has use inside the
QDataStream &operator<<(QDataStream &, const QXxx &);
andQDataStream &operator>>(QDataStream &, QXxx &);
operators.The data class I am writing has two parts: configuration and actual data, the configuration part is very small while the actual data part can be as large as required. Sometimes only configuration is supposed to be read from the stored data file on the hard disk, and similarly, sometimes only configuration is supposed to be written to the stored data file on the hard disk. This is done when serializing the data and is based on which part of my application is trying to read/write the data. The option to include the actual data part while serializing the data is passed to another class called
DataReaderWriter
when requesting a read/write from/to a file on the hard disk. TheDataReaderWriter
creates aQDataStream
class object and reads/writes the data file. Since theQDataStream &operator<<(QDataStream &, const QXxx &);
andQDataStream &operator>>(QDataStream &, QXxx &);
operator don't accept any additional inputs, I need to pass this option as a part of either theQDataStream
object or make it a part of my data class. But because this option has no use for any other parts of the application I wanted to add it like an additional property to theQDataStream
object just like we can add custom properties toQObject
usingbool QObject::setProperty(const char *name, const QVariant &value)
. -
@CJha Hi,
There's something that is not really clear in your design. Where are these configuration bits coming from and why should QDataStream care about them at all ?
QDataStream is only there to stream data to and from a binary file, nothing more. Any and all data specific processing should be done by the objects that you are going to load and store there.
Also, how do you expect to be able to only read configuration information sometimes and sometimes not ?
QDataStream does not offer to access your files at random points to load either configuration data or pure data.To me it seems that you are trying to mash two different things together that should be independent. You might want to consider using a file format like HDF5.
-
@SGaist Hi,
Also, how do you expect to be able to only read configuration information sometimes and sometimes not ?
QDataStream does not offer to access your files at random points to load either configuration data or pure data.
The actual data (i.e. the part that can be as big as required) is read/written at the very end of the streaming operation so that this part can become optional. The
<<
and>>
operator functions are designed so that it works in any scenario. So, reading and writing is not the problem, the problem is passing the information about if the actual data is supposed to be read/written or not.The actual data part can be big and if I am loading the file just for the configuration part then I will have to throw away the actual data part anyway after loading it. Usually reading just one single data file can take more than a few seconds, but if I skip the actual data part then the same operation can be done in less than 100 milliseconds. The same is true for writing the file. That's why I want to make the inclusion of the actual data an option when serializing the data using
<<
and>>
operator functions. -
@CJha
@SGaist may have a better answer than I. But in this case what about subclassingQDataStream
to allow you to add a member variable for what you want it to include/exclude and using that derived class as yourQDataStream
object? Or, if you like object properties, wrap it inside aQObject
so that you can set them, and write a streaming operator for your class which looks at that and decides whether to stream/unstream the encapsulated data class, which amounts to the same thing? I'm still not sure that is the right place to store this "behaviour attribute" on streaming, but it should work? -
@JonB Thanks! Yes, subclassing the
QDataStream
should work, I was hoping to avoid it though. The second option is to store the "behavior attribute" in the data class itself and I think I will make it this way, it's just that this variable is not required at all except for the<<
and>>
operator functions and so could cause confusion with other programmers later on, but it's nothing that proper documentation can't solve ;) -
@CJha
It does seem wasteful to store this "streaming attribute" in your objects' data class. If there are a million objects, why have a million bools in them to tell them what to do if it's all supposed to be the same? Not to mention, what would happen if some objects' boolean said to stream all information and some did not? And you don't want to have to change something inside each object every time you want to stream with or without the "actual data part". That's why it feels like an attribute on theQDataStream
rather than on the objects. Maybe you can have a static function on the objects to return your information instead of actual storage in each instance.