PDA

View Full Version : QStandardItem question.



alexandernst
20th July 2009, 23:47
I have a QStandardItemModel, I have some parent and some childs in the model.


child = self.model.item(parent).child(0)

This will give me the first child if the parent. The question is: How can I get the next child of parent?

Thanks

wysota
21st July 2009, 00:21
Eeem...

child = self.model.item(parent).child(1)
?

Actually it's probably better to do it like this:
QModelIndex firstChild = self.model.index(0, 0, parent)
QModelIndex secondChild = self.model.index(1, 0, parent)
QStandardItem firstItem = self.model.itemFromIndex(firstChild)
QStandardItem secondItem = self.model.itemFromIndex(secondChild)

etc.

alexandernst
21st July 2009, 00:30
Why did you changed the child(0) to child(1) ? Isn't child(0) the first element under parent?

I mean:

|-parent0
--child0
--child1
--child2
|-parent1
--child0
|-parent2
.............

I can't say how many parents/childs I'll have, so I can't use your method. I just need to be able to see if there is a "next" child in the current parent. Something like being in "parent0, child1" and ask for "next" child, then I'll get "parent0, child2". If I'm in "parent1, child0" and I ask for next child, I'll get None, null, 0 or somehing like that...

Is there any way of doing that?

Lykurg
21st July 2009, 00:46
I can't say how many parents/childs I'll have, so I can't use your method.
Qt is shipped with a very good documentation: Dare using it! -> QStandardItem::rowCount()


P.s.: And next time please also mind to use the Newbie section for questions like that

alexandernst
21st July 2009, 01:02
Ok, I could use a temp int and count the number of childs in every parent and then I'll just go to child(int), but what about parents? How can I go to the next parent?

wysota
21st July 2009, 01:48
Why did you changed the child(0) to child(1) ?
Because you wanted to access the second child. At least that's what I understood.


I can't say how many parents/childs I'll have, so I can't use your method.
Hmm? Why not?


I just need to be able to see if there is a "next" child in the current parent.
Ok, if a parent has four children and your at the second one, it is easy to deduce how many more are left. Furthermore if you access a non-existant element, you'll get an invalid index - so even if you didn't know about the QAbstractItemModel::rowCount() method you could still know if you've just fallen off the cliff.

BTW. Note also the QModelIndex::sibling() method.

alexandernst
21st July 2009, 02:01
Ok, I made it :)
I just did something like

parent = model.item(n_parent) (while n_parent gets incremented in a while)

Oh, wysota, I meant "go to the next element", but without knowing the current one. But nm, I think I found a solution ^^