PDA

View Full Version : Storing window geometry inside XML file.



GuS
16th July 2008, 18:23
Hi guys!

I wanted to know how to store the window geometry inside a XML file.

Actually i use QSettings, but i want to change to XML since i already have an xml preferences file on my application (made using QDom). So, i want to store the window geometry in there too.

Anyone could help me?

Thanks.

Cheers.

aamer4yu
16th July 2008, 18:24
I guess you know how to populate the QDomDocument

You can use QDomDocument::toString to get the string, and I hope saving a string in a file is not a tedious task :)

GuS
16th July 2008, 19:24
Yep.. but... mmm, in this case what i need is: write text on an existing tag, since i already have the tag created (using a method inside the xml implementation)

So, i must search the tag (I believe is with elementsByTagName) but then i don't know how to write in it. I know what i have to write, which is QVariant(self.saveGeometry()).

Sorry... but i am newbie in Qt and development world yet...

I will appreciate some help.

Thanks.

aamer4yu
17th July 2008, 05:56
Well for a hint you can open some ui file in Wordpad and see how it stores geometry.

In a simple way you can create a tag like this -


<Window id="1">
<Geometry x="" y="" width ="" height="" />
</Window>


For creating tags, you can use 2 ways -
1) Write xml content into a string and use QDomDocument.setContent()
2) Create each tag using QDomDocument::createElement(), and QDomElement::setAttribute().

Hope u can find ur way now :)

GuS
17th July 2008, 13:42
Hi,

Thank for the reply :)

Mmm thats looks like a nice idea, but i already know how to create tags inside xml... (which is not my problem in this case...)

The tag for the Window geometry is being created inside a method which i've created all necesary tag with their strings data inside, except for the window Geometry one that i create am empty string (so i could later write the window geometry at close event of my application)

My problem is, how to write inside an existing tag. I don't want to recreate the tag again, since already exist. But only modify the string of that tag every time i execute the close event (and not creating the hole xml documment, only creating or replacing the string inside the needed Tag)

What i did yesterday was:


self.file = QFile('preferences.xml')
self.file.open(QIODevice.ReadOnly)
self.doc = QDomDocument("OpenCoffeePreferences")
self.doc.setContent(self.file)
self.file.close()


self.docElem = self.doc.documentElement()
self.node = self.docElem.firstChild()

while (self.node.isNull() == False):
self.element = self.node.toElement()
if (self.element.isNull() == False):
if (self.element.tagName() == "windowGeometry"):

self.text = self.doc.createTextNode(QVariant(self.saveGeometry ()).toString())
self.element.appendChild(self.text)
self.node = self.node.nextSibling()

self.file.open(QIODevice.WriteOnly)
self.file.write(self.doc.toString().toAscii())
self.file.close()

But I dont know if this is right. What stores inside the xml is garbarge, like binary garbage.