PDA

View Full Version : How to get parent of selected item in TreeView?



RavenS
7th March 2009, 12:47
Hello, guys. At first, sorry for my bad english...

For my purposes I was created widget kinda "date picker", based on model/view architecture. Widget haves the next structure (for example):

2000
January
1
2
3
...
February
1
2
3
...
Other months...
2001
January
1
...
2002
2003
...
etc

User can pick a date. Not just a day, he can also pick a month, or a year.
It's so easy to get data of the selected item, just

value = model->data(treeWidget->currentIndex(), Qt::DisplayRole);
but how to get the parent of the selected item? In case when user selected a month or a day, we need to know, for which year belongs this month (or for which year and month belongs this day).
So, I need help :) How to get data of the parent of the selected item?

caduel
7th March 2009, 13:13
QTreeWidgetItem *item = treeWidget->currentItem();
value = item->data();
QTreeWidgetItem *item_parent = item->parent();
if (item_parent) ....


I suggest not to use the "DisplayRole" here.
I would add a custom role for each item that contains the QDate so you don't have to build up dates from displayed data. Maybe another role that tells you if it is a year/month/day item.
(You just have to figure out how you represent months and so on (a QDate can't have a day value of 0.)

HTH

RavenS
7th March 2009, 13:27
Huh, thank you very much! :) I just didn't saw this method. :D

And also thanks for suggestion, I'll think about it.