Hi everybody,
I'm developing a little 2d Graphics Modeling application using the Qt's Graphics View framework. Currently, I'm trying to find out how to Save and Load the state of a Scene.
I've been advised to store the data as an XML archive. So I thought I would just iterate over my QGraphicsScene's items gettin its properties and storing them with an appropriate tagging, like for example for an ellipse :
<elipse id="ellipse1">
<color> Black </color>
<width>50</width>
<height>50</height>
</ellipse>
<elipse id="ellipse1">
<color> Black </color>
<width>50</width>
<height>50</height>
</ellipse>
To copy to clipboard, switch view to plain text mode
After reading that I would create a QGraphicsEllipseItem with a Black contour bounded by a 50x50 rectangle and add it to the current scene that is being loaded. For storing I would iterate over the Scene's elements, get its properties and store it like the code above.
But I'm facing a little problem with the iteration over the tags using readNextStartElement() . Here's my main loop code:
//parser is a QXmlStreamReader object
while(!parser.isEndDocument()){
while(parser.readNextStartElement()){
if(parser.name() == "ellipse"){ //When it finds an <ellipse> tag
readEllipse();
}
else{
continue;
}
}
}
//parser is a QXmlStreamReader object
while(!parser.isEndDocument()){
while(parser.readNextStartElement()){
if(parser.name() == "ellipse"){ //When it finds an <ellipse> tag
readEllipse();
}
else{
continue;
}
}
}
To copy to clipboard, switch view to plain text mode
And the code for function readEllipse() :
void MyXmlReader::readEllipse(){
while(parser.readNextStartElement()){ //Should read all the tags untill </ellipse> is reached, so it can call appropriate readColor(),readHeight(),etc.
// readColor() or readHeight() or readWidth()
}
//Here all the properties of the ellipse would have already been parsed so we could add it to the actual scene:
parentQGraphicsScene->addItem( newEllipse);
}
void MyXmlReader::readEllipse(){
QGraphicsEllipseItem *newEllipse = new QGraphicsEllipseItem(); //Creates a new Ellipse which will later be added to the scene
while(parser.readNextStartElement()){ //Should read all the tags untill </ellipse> is reached, so it can call appropriate readColor(),readHeight(),etc.
// readColor() or readHeight() or readWidth()
}
//Here all the properties of the ellipse would have already been parsed so we could add it to the actual scene:
parentQGraphicsScene->addItem( newEllipse);
}
To copy to clipboard, switch view to plain text mode
The problem with this approach is that when I call readNextStartElement() in the main loop, I read the <ellipse> tag and then readEllipse() is called. Inside readEllipse(), the while(parser.readNextStartElement) starts reading <color> and keeps reading until it finds its end tag (i.e </color>) and so it stops, leaving the processing of <height> and <width> undone.
If it had a way to know wether I'm still inside <ellipse></ellipse> I guess I could just keep reading next start element untill it finds </ellipse> .
I'm a litle confused here since it's my first time parsing a XML file, so if could somebody help me figure this out, suggesting a simpler approach perhaps, I would be very grateful.
Thanks in advance,
Luiz.
Bookmarks