[Qt-interest] How to deal with pointers for a QDataStream?

Andre Somers andre at familiesomers.nl
Mon Apr 19 15:01:40 CEST 2010


On 19-4-2010 14:41, Oliver Heins wrote:
> I have a data structure which holds some pointers:
>
> class Token
> {
> public:
>      enum Type { Space, Paragraph, Kern, Char, Format, Note, End };
>      enum SubType { Bold, Italic,
>                     Header1, Header2, Header3, Header4, Header5, Header6,
>                     Itemize, Enumerate, Description, Item,
>                     Note1, Note2, Note3, Note4, Note5 };
>
>      [...]
>
>      Type type;
>      SubType subType;
>      QChar chr;
>      Token *parent;
>      QLinkedList<Token *>  children;
>      Token *partner;
> };
>
> As saving pointers directly to a QDataStream will obviously not work,
> what would be a good strategy to save the structure?
>
>    

One way is to give each item you need to store a pointer to an ID that 
is unique for the file (if such a thing is not already there), and store 
these ID's instead of the pointers to the objects. On loading, you can 
reconstruct the pointers by using the ID's to lookup the addresses from 
some (temporary?) data structure such as a QHash<int, Token*> that you 
keep around at loading time.

André




More information about the Qt-interest-old mailing list