View Full Version : Adapting the "Simple DOM Model Example" to be read/write

4th September 2008, 18:05
The example is found in the Qt Assistant. I'm trying to use a Tree View with an XML file as my model. I tried to search through these forums to see if something like this has been talked about, but you're not allowed to search for xml and "xml" as a tag has been used four times when I was searching.

Anyway, the example is read-only. What I'll be wanting to do is either just add comments to elements or to create brand new elements in the xml. I'm new to using Model/View stuff (I've always just used the much easier to use TreeWidget and such before), but I was wondering if someone has made an XML model that is read/write, some helpful tips/code snippets, or links to tutorials who can walk me through this problem.


4th September 2008, 19:58
If you want simply edit functionality, the simplest way would be to use QTextEdit and apply a syntax highlighter on it.

but then you wont be able to expand/collapse nodes.

Other thing you can try is setting delegate on the tree view, and edit the node. I had modeified the simple DOM model example applying delegate to it. You can refer this (http://www.qtcentre.org/forum/f-qt-programming-2/t-xml-highlighting-and-modelview-14288.html) thread. Hope it helps you :)

4th September 2008, 21:34
I don't want to just display the XML as it's read, but display it differently. Example:

Say I have a list of bikes. Each bike has the following elements,

Comfort Level
Top Speed

This list is pretty stupid, but it will get my point across hopefully. Instead of just showing a bike, then you unfold the bike to see it's elements, I want each row in the tree to be a bike and have each column be filled with the data found inside (first column be name, second be comfort). However, I want users to be able to update the underlying XML. Say they select three different bikes, I'd like each selected bike to get an attribute like, springcollection="true", or something along those lines.

How should I go about adapting the example to my needs, or should I begin building a brand new model? Has anyone done something like this before on these forums?

5th September 2008, 06:10
Delegates :)

see the spin box delegate example in the Qt Demos. Instead of spinbox u need lineedit.

5th September 2008, 15:45
I'm mostly trying to have quick and easy access to XML for both reading and writing. I'm guessing I could get by with just rapid reading of elements and then just give users the option to save at the end, which would limit how much I'd ever need to write. But the dream is to rapidly read/write from XML.

For my model, when I reimplemented the data function, is what I really want to do is go to the bike indexed by row, and look at the column to see which part of that bike I want to display? Is that how models work?

5th September 2008, 19:56
For my model, when I reimplemented the data function, is what I really want to do is go to the bike indexed by row, and look at the column to see which part of that bike I want to display? Is that how models work?

Yes. A row represents a structure, and columns their attributes .
What did u try till now ?

8th September 2008, 16:59
Sorry about getting back to you so slowly; I'm working on lots of different projects right now.

What I'm trying to do is use a DOM as the model for my data, and use a QTreeView as my view. However, lots of data in the model I want to be able to ignore. Certain nodes I want to be able to skip, while other nodes I want to have displayed all along the same row, but in separate columns.

Is there a way to easily handle this using Qt or an example of this somewhere, or will I have to really learn the Model/View well and solve this problem myself with very little outside help?

8th September 2008, 21:19
If I understand what you are trying to do, you will need to implement the logic of your application in the model. The view merely displays what you present to it from the model. As I recall, the simple example is displaying three columns that represent the name (tag name in the case of an element), attributes, and the node value (the string or comment contents of a text node.)

To make your bike program work correctly, you will need to build a model that understands the organization of your data and presents it to the view in the appropriate rows and columns. If you want to implement editing then you will need new delegate to edit within the treeview.