PDA

View Full Version : Hide Parent and show only children in QTreeView



y.s.bisht
18th January 2012, 10:13
Hi All,

I am stuck with a problem, I was wondering is there any way to just show children in a Qtreeview not it's parent.
For instance.

If this is the tree



AAA--
|
A _
|
a1
|
a2
|
a3
|
a4


I want only this to be shown, that is without Root node (AAA in this case)



A _
|
a1
|
a2
|
a3
|
a4



I searched a lot in net, but without success. If I hide the root row, I am not able to see it's children,I think there should be some way to do that .


Thanks in advance.

ChrisW67
18th January 2012, 10:29
QTreeView::setRootIndex() looks good to me.

wysota
18th January 2012, 10:29
QAbstractItemView::setRootIndex()

y.s.bisht
18th January 2012, 11:47
Thanks you for your reply...
I am doing the same thing in my code but has no effect...

MyModel* model = new MyModel( ui->treeView ); //MyModel inherits QAbstractItemModel
ui->treeView->setModel(model);
ui->treeView->setRootIndex(model->index(2,0,QModelIndex()));

I want to make third row my parent.


7285

wysota
19th January 2012, 01:05
It has no effect because your model has only one row in its top-most level of the hierarchy.

y.s.bisht
19th January 2012, 09:21
Thanks wysota,
when I changed

ui->treeView->setRootIndex(model->index(2,0,QModelIndex()));

to

ui->treeView->setRootIndex(model->index(0,0,QModelIndex()));

First root become invisible, but is there anyway to make second child of root, main root ?

wysota
19th January 2012, 10:41
You can make any index the root index if you pass it to setRootIndex().

QModelIndex grandparentIndex = model->index(0,0);
QModelIndex parentIndex = model->index(0,0, grandparentIndex);
QModelIndex idx = model->index(2, 0, parentIndex);
ui->treeView->setRootIndex(idx);

ChrisW67
19th January 2012, 10:45
There is no second child of the root in your example... that's the point. The third row in your display is the first child of the first child of the topmost item.


Edit: Yeah, what wysota said.

wysota
19th January 2012, 10:51
Yeah, of course my code won't work with this exact model because there is no third child of the first child of the first child of the model. But I hope OP gets the picture now.