PDA

View Full Version : how to use QTreeView::setFirstColumnSpanned to span a child item



startyourengine
15th August 2012, 18:15
hello

i have a treeview in the following structure:

parent1

child10

child11
parent2

child20

child21
parent3

the following code works and it spanned all the parents that has a child, so in this case parent1 and parent2 get spanned, parent3 doesn't.


QModelIndex idx = model->invisibleRootItem()->index();
for (int i=0; i<model->rowCount(); i++){
if (model->item(i,0)->hasChildren()){
Treeview->setFirstColumnSpanned(i, idx, true);
}
}


but what I'm actually trying to do is to get the children's first column spanned, so I thought (const QModelIndex & parent) the second parameter of setFirstColumnSpanned takes the parent index of the item that I want to span. so I replace

Treeview->setFirstColumnSpanned(i, idx, true)
with

Treeview->setFirstColumnSpanned(i, model->Item(i,0)->index(), true)
but it doesn't work.

can somebody point me to the right direction? or is this doable? thank you.

ChrisW67
16th August 2012, 05:15
"it doesn't work" is not a good description of the problem.

The row number of the child within its parent and the index of the parent are required in the call to setFirstColumnSpanned().
For each first level item with children you want to iterate over all of its children set their first column spanned. This is two loops.