PDA

View Full Version : QTreeView adding children to the view



Tux-Slack
16th October 2007, 21:20
Hello there.

I have a QTreeView and am using a QStandardItemModel to fill that tree view. I allready have a constructed read/write list of data but would need to implement adding children to particular items as well.
I tried a few ways, but just can't get it.

This is how I'm adding items:

QModelIndex pI;

model->insertRow(itemCount, QModelIndex());
pI = model->index(itemCount,0,QModelIndex());
model->setData( pI, title, Qt::DisplayRole);
model->item(itemCount, 0)->setEditable( false );

model is a QStandardItemModel pointer and title is a QString variable.

And this is how I tried to add a child to only one item:

QModelIndex cI;

model->insertRow(1, QModelIndex());
cI = model->index(1, 0, QModelIndex());
model->setData( cI, "test", Qt::DisplayRole);
model->item(1, 0)->setChild(0, 1, model->item(5,0));
The list is filled with data and then this piece of code get's executed.
But what happens is that another item is added("test") and an empty child is assigned to it. But I would like to assign this "test" to an existing item on second row in the list.

I did read the docs up and down but can't figure it out.

Tux-Slack
17th October 2007, 20:57
Anybody? :/

jpn
20th October 2007, 09:28
Perhaps you want something like this:


QStandardItem* parent = model->item(row);
if (parent)
{
QStandardItem* child = new QStandardItem("test");
parent->appendRow(child);
}

You can also pick the parent like this (if you want to add a child under current index at runtime):

QStandardItem* parent = model->itemFromIndex(view->currentIndex());