Results 1 to 2 of 2

Thread: Need help Updating QTreeView model (QAbstractItemModel)

  1. #1
    Join Date
    Oct 2009
    Location
    Maryland
    Posts
    16
    Thanks
    3
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Unhappy Need help Updating QTreeView model (QAbstractItemModel)

    I can't get new data to show up in my QTreeView. I'm using Qt 4.5.
    My application is a modified version of the "editabletreemodel" demo code.
    I'm trying to insert more tree data at the root of the tree being displayed.

    When the application starts up, a QTreeView is created with a TreeModel
    (derived from QAbstractItemModel) which has no children. Everything looks
    fine: column headers are properly displayed (which are retrieved from the
    fields of the root element), etc.

    When the user imports data, the file parser produces a tree of TreeItems.
    To add this to the root of the tree being displayed by the TreeModel I created
    the following method on TreeModel:
    Qt Code:
    1. addAtRoot(TreeItem *moreData)
    2. {
    3. int position = rootItem->childCount();
    4.  
    5. QModelIndex modelIndex = createIndex(0, 0, rootItem);
    6.  
    7. beginInsertRows(modelIndex, position, position); // adding 1 thing
    8. rootItem->insertChildItem(moreData, position);
    9. endInsertRows();
    10. }
    To copy to clipboard, switch view to plain text mode 

    The "insertChildItem()" method is implemented in the TreeItem class and does this:
    Qt Code:
    1. void TreeItem::insertChildItem(TreeItem *item, int position)
    2. {
    3. item->parentItem = this;
    4.  
    5. childItems.insert(position, item);
    6. }
    To copy to clipboard, switch view to plain text mode 

    Using the debugger, I can see that the tree of TreeItem objects available through the
    TreeModel is properly updated. However, the QTreeView never displays the new data.

    Any help would be appreciated.

    Am I not constructing the right QModelIndex in addAtRoot()? Is there something
    I need to do to notify the QTreeView besides calling (begin/end)InsertRows()?

    Notes:
    One interesting item of note is that if I invoke the "insert row" function to create a new,
    (blank) item, it gets a sizeways "T" line rendering instead of an "L" shaped
    line in the heirarchy view. This tells me that at least part of the QTreeView
    knows there was data added.

    If I replace the call to addAtRoot() with a call to insertRows() I can create a
    new (empty) item in the tree which is displayed properly.

    To try and debug this, I changed the code so that the TreeModel isn't created
    and associated with the QTreeView until the file is imported. This exercised
    the TreeItem "insertChildItem()" method, but not the TreeModel "addAtRoot()"
    method. The result worked fine. It limits the user to a single input file,
    which isn't acceptable for my application.
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need help Updating QTreeView model (QAbstractItemModel)

    You surely shouldn't use createIndex() here... Seems that you should pass an invalid index there. If it works then it could mean that your QAbstractItemModel::parent() implementation is wrong so be sure to check that one as well. Although on the other hand you're trying to pass valid parameters to createIndex() which is also wrong. If you're adding at the root, you should pass an invalid index (i.e. createIndex(-1,-1,0) if at all but it's easier to simply pass QModelIndex()).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    iraytrace (2nd December 2009)

Similar Threads

  1. QTreeView and custom model from QAbstractItemModel
    By croscato in forum Qt Programming
    Replies: 5
    Last Post: 18th January 2010, 16:03
  2. One Model, Two Views (QTreeView and QTableView)
    By dgarrett97 in forum Newbie
    Replies: 2
    Last Post: 14th September 2009, 18:10
  3. Updating QTreeView with custom model
    By supergillis in forum Qt Programming
    Replies: 8
    Last Post: 18th September 2008, 07:01
  4. Creating a QAbstractItemModel for QTreeView
    By hbill in forum Qt Programming
    Replies: 13
    Last Post: 14th August 2008, 16:01
  5. Modify model data in QTreeView
    By YuriyRusinov in forum Qt Programming
    Replies: 6
    Last Post: 26th October 2006, 17:28

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.