PDA

View Full Version : QStandardItemModel, parents and childs



alexandernst
22nd July 2009, 20:01
How can I make this:

|-parent0
--child0
--child1
|-parent1

The code for adding the parents is


model = QStandardItemModel()
model.appendRow(QStandardItem("parent0"))
model.appendRow(QStandardItem("parent1"))

and for the childs?

I have this and I'm not sure that it's working:


model.item(0).apprenRow(QStandardItem("child0"))

Thanks

wysota
22nd July 2009, 20:09
What do you mean you are not sure if that's working? Can you see the child0 item or not?

Lykurg
22nd July 2009, 20:13
and for the childs?

I have this and I'm not sure that it's working:

Hmm, if you are not sure, if it's working, why don't you simply try and see if it's working:confused: And beside, a look into the docs are always good:
An example usage of QStandardItemModel to create a tree:
QStandardItemModel model;
QStandardItem *parentItem = model.invisibleRootItem();
for (int i = 0; i < 4; ++i) {
QStandardItem *item = new QStandardItem(QString("item %0").arg(i));
parentItem->appendRow(item);
parentItem = item;
}

alexandernst
22nd July 2009, 20:30
In fact, I'm trying to test it, but I cant see it :(
And... my code is a little bit different.
I have a QStandardItemModel with 5 columns per item. 2 and 3 column are ID's.



item = self.__search_by_id(id) #this will return a QStandardItem matching the ID arg
item.appendRow([None, None, QStandardItem(var), QStandardItem(var2), QStandardItem(True)])


I cant see the childs... Maybe I'm doing something wrong? (doc's says that everything is fine)

wysota
22nd July 2009, 21:20
I'm not sure how appending a null would work... There should be 5 items here, some of them may be empty but they should still be there. And I think you need to tell the parent item that its children are to have 5 columns - it's not enough to do that on the top-level.

alexandernst
22nd July 2009, 21:22
Mmm, ok, so... how could I do that? could you give me an example or a link? (link != pyqt docs if possible, I already have that one xD)

wysota
22nd July 2009, 21:24
To do what exactly? To set the column count of children to 5? QStandardItem::setColumnCount()

alexandernst
23rd July 2009, 00:00
nm, I did it :)

It was just simple as

child.appendRow([.....])

But thanks anyway!