[Qt-interest] How to save the content in tree structure to file
Andre Somers
andre at familiesomers.nl
Tue May 4 12:54:11 CEST 2010
Hi,
On 4-5-2010 11:39, Ramesh wrote:
>
> Ok.. I'm sorry,
>
> I know how to use Qdatastream , which we use to write the data to
> file.. but its not serialized..
>
QDataStream does data serialization. What do you mean if you say "it is
not serialized"?
>
> The way you write is the same wy you need to access the data from the
> system..
>
I have no idea what you mean by this.
>
> But in tree hierarchy, I need the key value pairs.. I need to know
> which one is parent and child..
>
> How can I do it.. by using Qdatastream?
>
You can use QDataStream to stream any file structure you like.
QDataStream does not do the structuring for you, unless the class you
are streaming out already contains such structure and it supports the
QDataStream stream operators << and >>, such as QList or QHash do. What
do you mean by "the key value pairs"? Which pairs?
You don't talk about how your data is structured in your program.
Unfortunately, Qt does not supply a QTree<> container. So, I guess you
are using some other structure. Could you tell us what you have now?
I would probably do something according to these lines:
The first step is a format for the tree itself. I would choose to stream
out the number of items at top level in the tree as an unsigned int
(quint32), followed by each of the top level tree items. That is really
all you need.
How each item looks exactly, depends on what data you want to store in
each tree item. In the example below, I will assume only a name that is
stored in a QString and an unsigned integer ID. Each tree item is then
an instance of a class, like this (all code is pseudo-code):
class TreeItem {
unsigned int id;
QString name;
QList<TreeItem> children();
unsigned int childCount();
}
If you want to write that to a file using QDataStream, you could do
something like this:
QDataStream operator()<<(QDataStream& out, const TreeItem& item) {
out << quint32(item.id);
out << item.name;
out << quint32(item.childCount());
foreach(TreeItem, item.children()) {
out << item; // note: this is a recursive call!
}
return out;
}
That is, you have each TreeItem write out its own data, the number of
its children, and then recursively write out each of its children.
Reading back is the opposite process, where you need to be VERY CAREFULL
that the procedures work on exactly the same structure, with the same
data types.
If you would considder not using QDataStream (you are not telling why
you want to use it), you could perhaps also considder the suggestion by
Srdjan to use XML. It still requires you to do your own reading and
writing somewhere, so I am not sure if it is a benefit for you.
Personally, I think XML is nice, but mainly if you:
* need to have the data sort-of human readable, or
* need to have interoperability and extensibility.
André
> *From:* qt-interest-bounces at trolltech.com
> [mailto:qt-interest-bounces at trolltech.com] *On Behalf Of *Andre Somers
> *Sent:* Tuesday, May 04, 2010 3:01 PM
> *To:* qt-interest at trolltech.com
> *Subject:* ***SPAM*** Re: [Qt-interest] How to save the content in
> tree structure to file
>
> On 4-5-2010 11:25, Ramesh wrote:
>
> Hi,
>
> I want to save the data to a file in tree hierarchy, Same way I want
> to display it on UI.
>
> How can I achieve that in Qt..
>
> This question can not be answered the way you formulate it.
> Please read and understand
> http://catb.org/~esr/faqs/smart-questions.html
> <http://catb.org/%7Eesr/faqs/smart-questions.html>, and reformulate
> your question.
>
> André
>
>
> -----------------------------------------------
>
> Robosoft Technologies - Come home to Technology
>
> Disclaimer: This email may contain confidential material. If you were
> not an intended recipient, please notify the sender and delete all
> copies. Emails to and from our network may be logged and monitored.
> This email and its attachments are scanned for virus by our scanners
> and are believed to be safe. However, no warranty is given that this
> email is free of malicious content or virus.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100504/3ab79aeb/attachment.html
More information about the Qt-interest-old
mailing list