Results 1 to 6 of 6

Thread: QTreeView and custom model from QAbstractItemModel

  1. #1
    Join Date
    Oct 2008
    Location
    Brasil - São Paulo - Marília
    Posts
    28
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    3

    Default QTreeView and custom model from QAbstractItemModel

    Hi all!

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

    Qt Code:
    1. QTreeNode* root = new QTreeNode("Root", 0);
    2.  
    3. QTreeNode* n1 = new QTreeNode("N1", root);
    4.  
    5. QTreeNode* n2 = new QTreeNode("N2", root);
    6. QTreeNode* n21 = new QTreeNode("N21", n2);
    7. QTreeNode* n22 = new QTreeNode("N22", n2);
    8. QTreeNode* n23 = new QTreeNode("N23", n2);
    9.  
    10. QTreeNode* n3 = new QTreeNode("N3", root);
    11.  
    12. QTreeModel* model = new QTreeModel(this);
    13.  
    14. model->setRootNode(root);
    15.  
    16. treeFolders->setRootIsDecorated(true);
    17. treeFolders->setModel(model);
    To copy to clipboard, switch view to plain text mode 

    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!
    Attached Files Attached Files

  2. The following user says thank you to croscato for this useful post:

    geageagea (25th July 2011)

  3. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTreeView and custom model from QAbstractItemModel

    just insert an additional "pseudo-root" item as the first item and your root as its child

  4. #3

    Default Re: QTreeView and custom model from QAbstractItemModel

    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:isplayRole)
    {
    // return QVariant();
    return QIcon("act.png");
    }
    QTreeNode* node = nodeFromIndex(index);

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

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

    return QVariant();
    }

  5. #4
    Join Date
    Oct 2009
    Location
    Mexico
    Posts
    81
    Thanks
    6
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView and custom model from QAbstractItemModel

    Quote Originally Posted by PeterEddy93 View Post
    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:isplayRole is used for display text. for display a icon in the view, use the Qt:ecorationRole. 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

  6. #5

    Default Re: QTreeView and custom model from QAbstractItemModel

    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

  7. #6
    Join Date
    Apr 2009
    Posts
    36
    Thanks
    1
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView and custom model from QAbstractItemModel

    Quote Originally Posted by Jack Lee View Post
    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.

Similar Threads

  1. Custom context menu in QTreeView
    By ttvo in forum Qt Programming
    Replies: 5
    Last Post: 3rd April 2009, 23:29
  2. Updating QTreeView with custom model
    By supergillis in forum Qt Programming
    Replies: 8
    Last Post: 18th September 2008, 08:01
  3. Replies: 5
    Last Post: 6th December 2007, 15:43
  4. Treeview and custom model
    By steg90 in forum Qt Programming
    Replies: 8
    Last Post: 15th May 2007, 14:54
  5. Add custom QTreeView in VS .NET
    By wind in forum Newbie
    Replies: 4
    Last Post: 5th October 2006, 18:04

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.