PDA

View Full Version : My first model/view attempt



Morea
15th September 2007, 22:25
I wish to create something vith a treeview. The top items should be of class A, and second level of class B. I will have a button to add items of class A, and I think that when I right click on an Item A, I will be able to add a B object to that branch.

What classes would you recomend me using for this?

Btw. Is there any pdf version of the docs at docs.trolltech.com? I prefer reading their model/view docs on paper and printing them directly from webpages doesn't look so very nice.

marcel
15th September 2007, 22:39
See QTreeWidget, QTreeWidgetItem and QTreeWidget::addTopLevelItem and QTreeWidgetItem::addChild (for adding B objects via A context menu).
And no, t
here is no pdf version.

Morea
16th September 2007, 08:06
I see that there is both QTreeWidget and QTreeView. Item based versus Model based. Which one should you use in what sitution?

marcel
16th September 2007, 08:51
QTreeWidget is derived from QTreeView, so under the hood it is still model based.
You should use QTreeView when you want more control over the model and/or item delegate.

fullmetalcoder
16th September 2007, 09:02
QTreeWidget is derived from QTreeView, so under the hood it is still model based.
It's still model based that's for sure but the interaction with the model is wicked... It is practically impossible to play with model, programmatically I mean, without using the item based approach (which soon does not fit if you want to have some control over your data...)


You should use QTreeView when you want more control over the model and/or item delegate.
QTreeView allows you to use any kind of data as a "source" for the model. Views interact with the model data through QModelIndex, delivered by the model, and used to query data from it later on. This approach would easily allow you to have your objects of class A and B stored somewhere (accessible to the model) while using them to show some content within a tree view. all you'd need is subclassing QAbstractItemModel according to the guidelines and this is easy enough. :)

Morea
16th September 2007, 11:51
I'm reading this, but exactly how do I glue everything together with the data? Should I store all the data in object A inside the customized modell or store it elsewhere and just keep a pointer in the modell?

There are more stuff in the program that should interct with the modell.
:confused::o:eek: Well, I don't blame you if you do not understand this question. I sure don't.
I guess I have to go back to reading.

Morea
16th September 2007, 12:31
What I meant was, when I click on something in the view, I want the object that this view corresponds to in the model, to send signals to update other widgets. How do you do this in a nice clean way?

jpn
16th September 2007, 12:32
Perhaps this wiki article helps: QAbstractItemModel (http://wiki.qtcentre.org/index.php?title=QAbstractItemModel)

Morea
16th September 2007, 16:37
Well, I have written some code now.
But I still think about the structure. I will have a single top level object of class Solution, and this one will have a lot of children of type Table. I'm not really clear on how good it will be, but time will tell.

Anyway, I think I added a new Solution object (by using the File menu) but nothing is displayed.
Any hits would be very helpful.
Code here http://taljaren.se/kod.tgz

wysota
16th September 2007, 21:47
Maybe it's not too late to add my five cents - I suggest you start with QStandardItemModel, maybe it fits your needs and you won't need to implement your own model.

Morea
17th September 2007, 06:30
Your cents are always very valuable so it's not to late.
I'll have a look at the standard item.
Do you have any cents on how you put different objects in the tree?
It could look like this


A
|
+-B
+-D
|
+-D
+-B
+-D
|
+-E
+-B
|
+-C
+-F
|
+-F
+-C
+-F

Should I put generic elements like this
Node
{
int type; // A,B,C,D,E,
baseclass* obj; // pointer to either A,B,C,D,E,F object
}

Or how do you do this?:confused:

wysota
17th September 2007, 09:50
It depends whether you want to implement a model from scratch or use one of the already existing. For the former you can do what you want. For the latter I suggest you just use QStandardItem objects or similar. How much data do you want to store in each node and do you already have some representation of the data tree anywhere?