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. Custom property for QDataStream?
QtWS25 Last Chance

Custom property for QDataStream?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qdatastream
8 Posts 3 Posters 574 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.
  • C Offline
    C Offline
    CJha
    wrote on 21 Apr 2023, 09:16 last edited by CJha
    #1

    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 the QDataStream class, i.e. set something similar to bool QObject::setProperty(const char *name, const QVariant &value) but on the QDataStream 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 the QDataStream and QIODeviceBase class documentation and I could not find any way to add a property to QDataStream or to pass additional arguments when using the operators QDataStream& operator<< and QDataStream& operator>>.

    J 1 Reply Last reply 21 Apr 2023, 09:37
    0
    • C CJha
      21 Apr 2023, 09:16

      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 the QDataStream class, i.e. set something similar to bool QObject::setProperty(const char *name, const QVariant &value) but on the QDataStream 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 the QDataStream and QIODeviceBase class documentation and I could not find any way to add a property to QDataStream or to pass additional arguments when using the operators QDataStream& operator<< and QDataStream& operator>>.

      J Offline
      J Offline
      JonB
      wrote on 21 Apr 2023, 09:37 last edited by
      #2

      @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?

      C 1 Reply Last reply 21 Apr 2023, 09:54
      0
      • J JonB
        21 Apr 2023, 09:37

        @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?

        C Offline
        C Offline
        CJha
        wrote on 21 Apr 2023, 09:54 last edited by CJha
        #3

        @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 &); and QDataStream &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. The DataReaderWriter creates a QDataStream class object and reads/writes the data file. Since the QDataStream &operator<<(QDataStream &, const QXxx &); and QDataStream &operator>>(QDataStream &, QXxx &); operator don't accept any additional inputs, I need to pass this option as a part of either the QDataStream 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 the QDataStream object just like we can add custom properties to QObject using bool QObject::setProperty(const char *name, const QVariant &value).

        S 1 Reply Last reply 21 Apr 2023, 10:06
        0
        • C CJha
          21 Apr 2023, 09:54

          @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 &); and QDataStream &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. The DataReaderWriter creates a QDataStream class object and reads/writes the data file. Since the QDataStream &operator<<(QDataStream &, const QXxx &); and QDataStream &operator>>(QDataStream &, QXxx &); operator don't accept any additional inputs, I need to pass this option as a part of either the QDataStream 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 the QDataStream object just like we can add custom properties to QObject using bool QObject::setProperty(const char *name, const QVariant &value).

          S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 21 Apr 2023, 10:06 last edited by
          #4

          @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.

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

          C 1 Reply Last reply 21 Apr 2023, 10:26
          1
          • S SGaist
            21 Apr 2023, 10:06

            @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.

            C Offline
            C Offline
            CJha
            wrote on 21 Apr 2023, 10:26 last edited by CJha
            #5

            @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.

            J 1 Reply Last reply 21 Apr 2023, 10:34
            0
            • C CJha
              21 Apr 2023, 10:26

              @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.

              J Offline
              J Offline
              JonB
              wrote on 21 Apr 2023, 10:34 last edited by JonB
              #6

              @CJha
              @SGaist may have a better answer than I. But in this case what about subclassing QDataStream to allow you to add a member variable for what you want it to include/exclude and using that derived class as your QDataStream object? Or, if you like object properties, wrap it inside a QObject 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?

              C 1 Reply Last reply 21 Apr 2023, 11:12
              0
              • J JonB
                21 Apr 2023, 10:34

                @CJha
                @SGaist may have a better answer than I. But in this case what about subclassing QDataStream to allow you to add a member variable for what you want it to include/exclude and using that derived class as your QDataStream object? Or, if you like object properties, wrap it inside a QObject 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?

                C Offline
                C Offline
                CJha
                wrote on 21 Apr 2023, 11:12 last edited by
                #7

                @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 ;)

                J 1 Reply Last reply 21 Apr 2023, 11:22
                0
                • C CJha
                  21 Apr 2023, 11:12

                  @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 ;)

                  J Offline
                  J Offline
                  JonB
                  wrote on 21 Apr 2023, 11:22 last edited by JonB
                  #8

                  @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 the QDataStream 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.

                  1 Reply Last reply
                  1

                  5/8

                  21 Apr 2023, 10:26

                  • Login

                  • Login or register to search.
                  5 out of 8
                  • First post
                    5/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved