View Full Version : Tree model examples

18th October 2009, 11:49
I am trying to create a tree based model in Qt4, and I am getting into problems. I started by making a class which can load this new format, and so far so good. Then I assumed it will be easy to "stick" it into a model, but I then found that model more or less demand that you use QObjects (or use some other method for guessing the parent).

So I started, and wrote in a paper set of classes and demo functions that will be a QObject connector (proxy pattern, more or less) that copies the data internally. Then I realized I can have less code by eliminating the original code, and doing this completely in QObjects.

But now I see that I am linking myself to Qt4 only... and from a pure software engineering point of view this is wrong.

Now to do this right, I need to look into 3-5 real-life examples, or demos for MVC in Qt4. Can anyone point me to some good examples? The ones in Qt4 are not what I am looking for. Well... another option might be porting the format to XML internally, and then the mode will become easy as there are examples in Qt4 for for this kind of models... but I am not that happy about this scenario.

18th October 2009, 11:55
What exactly is the problem? Could you show us with a simple example? What's exactly wrong with examples bundled with Qt?

18th October 2009, 21:01
I am currently looking at 2 examples:

Simple DOM model - I assume I can load my data into QDomDocument and then move from there.... this will give me out of the box the "export to xml" feature.. .which is nice... but somewhat over killl..
Simple Tree Model Example - it's more or less what I am looking for... but it implies that my code will be much more... "heavier", instead of the light code I wrote originally. It feels like I am re-designing my code to work with MVC, and IMHO that breaks the idea that the data should not be restricted by the view, or model. I assume "blah... shut up and code..."

One implies re-writing my model to be tied with DOM, which as I said has it's benefits. The other screams at me "use QObject and get rid of it!". I was wondering what other developers have done. Is there another option?

18th October 2009, 21:39
If you already have the data, why move it to a different format? Simply implement the QAbstractItemModel interface for it and you're done.

18th October 2009, 23:06
In my original design I don't have "parents", only "childs" (not even directly, since most of my data members are POD and not classes).

I got stucked while trying to return the parent() inside the model... so I came back to the design board.

19th October 2009, 00:44
Try the solution I described at the end of this article: http://doc.trolltech.com/qq/qq25-undo.html