Hide Parent and show only children in QTreeView
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
Code:
AAA--
|
A _
|
a1
|
a2
|
a3
|
a4
I want only this to be shown, that is without Root node (AAA in this case)
Code:
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.
Re: Hide Parent and show only children in QTreeView
Re: Hide Parent and show only children in QTreeView
1 Attachment(s)
Re: Hide Parent and show only children in QTreeView
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.
Attachment 7285
Re: Hide Parent and show only children in QTreeView
It has no effect because your model has only one row in its top-most level of the hierarchy.
Re: Hide Parent and show only children in QTreeView
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 ?
Re: Hide Parent and show only children in QTreeView
You can make any index the root index if you pass it to setRootIndex().
Code:
QModelIndex parentIndex
= model
->index
(0,
0, grandparentIndex
);
ui->treeView->setRootIndex(idx);
Re: Hide Parent and show only children in QTreeView
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.
Re: Hide Parent and show only children in QTreeView
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.