PDA

View Full Version : QTreeView and custom model from QAbstractItemModel



croscato
24th July 2009, 21:25
Hi all!

I have created a custom model subclassing a QAbstractItemModel and inserted items with the following code:


QTreeNode* root = new QTreeNode("Root", 0);

QTreeNode* n1 = new QTreeNode("N1", root);

QTreeNode* n2 = new QTreeNode("N2", root);
QTreeNode* n21 = new QTreeNode("N21", n2);
QTreeNode* n22 = new QTreeNode("N22", n2);
QTreeNode* n23 = new QTreeNode("N23", n2);

QTreeNode* n3 = new QTreeNode("N3", root);

QTreeModel* model = new QTreeModel(this);

model->setRootNode(root);

treeFolders->setRootIsDecorated(true);
treeFolders->setModel(model);

What I get as result is:
|-N1
|-N2
| |-N21
| |-N22
| |-N23
|-N3

How can I manage to get it this way:
Root
|-N1
|-N2
| |-N21
| |-N22
| |-N23
|-N3

I have attached a file containing my model implementation if needed for
further examination.

Thanks in advance!

caduel
24th July 2009, 21:42
just insert an additional "pseudo-root" item as the first item and your root as its child

PeterEddy93
8th January 2010, 18:45
Hi,

I'm trying to add icons to the treeview by changing the data method in the QTreeView.cpp so each node has a icon with it. Can anyone help me with this.

Thanks
Pete.

QVariant QTreeModel::data(const QModelIndex& index, int role) const
{
if (role != Qt::DisplayRole)
{
// return QVariant();
return QIcon("act.png");
}
QTreeNode* node = nodeFromIndex(index);

if (node == 0)
return QVariant();

if (index.column() == 0) {
return node->name();
}

return QVariant();
}

ecanela
8th January 2010, 20:22
Hi,

I'm trying to add icons to the treeview by changing the data method in the QTreeView.cpp so each node has a icon with it. Can anyone help me with this.



Qt::DisplayRole is used for display text. for display a icon in the view, use the Qt::DecorationRole. this is the behavier espected from views.

another way is use a custom ItemDelegate class to draw each item. but the first approach is more simple

Jack Lee
18th January 2010, 12:04
Hi,

I'm using this QTreeModel and I am dynamically adding QTreeNodes to the treeview with user mouse clicks. I notice the treeview only updates when the user closes and then expands the tree.
I would like the treeview to immediately update with the newly added node and if possible expand to this node.

Can you offer some advice on how to do this

thanks
Jack

bmhautz
18th January 2010, 17:03
Hi,

I'm using this QTreeModel and I am dynamically adding QTreeNodes to the treeview with user mouse clicks. I notice the treeview only updates when the user closes and then expands the tree.
I would like the treeview to immediately update with the newly added node and if possible expand to this node.

Can you offer some advice on how to do this

thanks
Jack

How are you inserting the item into the tree? Are you calling insertRows/insertRow? If you are calling insertRows, then are you calling beginInsertRows and endInsertRows?

It sounds like you are adding the item to the tree data structure, but not updating the view appropriately. So, the item only gets drawn when it needs to draw the parent expansion (at this point, it would see the item).

If this doesn't help, please post some code so that we can take a look at it.