Results 1 to 5 of 5

Thread: QTreeView with QAbstractItemModel

  1. #1
    Join Date
    Dec 2009
    Posts
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default QTreeView with QAbstractItemModel

    I've subclassed both of the subject line classes in order to create a simple tree where the user can drag and drop the nodes to rearrange the tree. Everything was working fine until I wanted to essentially "reload" the tree (in case of a database change, for instance). Now the program is unstable and will crash at random, I believe because my QModelIndex.internalPointer()s become invalid (I found one pointing to a random place in memory in the QAbstractItemModel.parent() implementation).

    The thing is, I am not saving QModelIndexes anywhere in my code and the program is crashing in the event loop during calls to parent(). So I do not understand how the pointers become invalid.

    To reload the tree, I am deleting and recreating the underlying tree entirely and then emitting signal layoutChanged(). I feel this must be where I am messing up.

    Anyway, I have no idea where to look really, and I can't just post my entire code. How would you go about switching the underlying tree?

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView with QAbstractItemModel

    Are you emitting layoutAboutToBeChanged too? You need both. This one before you make the changes, and layoutChanged afterwards.

  3. #3
    Join Date
    Dec 2009
    Location
    New Hampshire, USA
    Posts
    13
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTreeView with QAbstractItemModel

    To reload the tree, I am deleting and recreating the underlying tree entirely and then emitting signal layoutChanged().
    Are you actually deleteing the tree (view) or the QAbstractItemModel? Or are you just setting the root node of the QAbstractItemModel to a new root node?

  4. #4
    Join Date
    Dec 2009
    Posts
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView with QAbstractItemModel

    I am using layoutAboutToBeChanged().

    Here's what I'm doing. It's PyQt. I have a tree made of TreeItem()s (my node class)

    Qt Code:
    1. # in subclass of QAbstractItemModel
    2. def recreateTree(self):
    3. self.emit(QtCore.SIGNAL('layoutAboutToBeChanged()'))
    4. self.root = TreeItem(None)
    5. # omitted code to generate the other nodes and add them to root
    6. self.emit(QtCore.SIGNAL('layoutChanged()'))
    To copy to clipboard, switch view to plain text mode 

    When I reassign self.root, Python automatically deletes the old tree (or should) since there's no other reference to it.

  5. #5
    Join Date
    Dec 2009
    Posts
    4
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: QTreeView with QAbstractItemModel

    After looking through the API a bit more I've changed my function a bit and it *seems to be working*. I really hate this feeling though, where I do not really understand what the API is doing behind my back.

    Qt Code:
    1. # in subclass of QAbstractItemModel
    2. def recreateTree(self):
    3. self.root = TreeItem(None)
    4. # [omitted code to generate the other nodes and add them to root]
    5. self.reset() # default implementation. I don't really know what it does
    To copy to clipboard, switch view to plain text mode 
    Last edited by Fletcher; 11th December 2009 at 00:21.

Similar Threads

  1. QTreeView and custom model from QAbstractItemModel
    By croscato in forum Qt Programming
    Replies: 5
    Last Post: 18th January 2010, 17:03
  2. Need help Updating QTreeView model (QAbstractItemModel)
    By iraytrace in forum Qt Programming
    Replies: 1
    Last Post: 26th October 2009, 23:49
  3. Replies: 0
    Last Post: 20th October 2009, 01:57
  4. Creating a QAbstractItemModel for QTreeView
    By hbill in forum Qt Programming
    Replies: 13
    Last Post: 14th August 2008, 17:01
  5. [QT4] QTreeView, QAbstractItemModel and sorting
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2006, 21:16

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.