Results 1 to 1 of 1

Thread: QAbstractItemModel::beginRemoveRows/endRemoveRows

  1. #1
    Join Date
    Jul 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Red face QAbstractItemModel::beginRemoveRows/endRemoveRows

    I've been using Qt for over a year and I really like this software, however I have some big issues concerning the removal of rows in a tree model.

    I've created a tree structure,
    Qt Code:
    1. class Node
    2. {
    3. weak_ptr<Node> parent;
    4. std::vector<shared_ptr<Node>> children;
    5. QString name;
    6. }
    To copy to clipboard, switch view to plain text mode 
    and implemented QAbstractItemModel to display the name in a QTreeView. The view correctly displays the data and I can easily add new rows, however removing them causes crashes. This is because during the painting process, the view calls my model with QModelIndexes that should have been removed (this happens after beginRemoveRows/endRemoveRows). Currently I rely on the fact that when I am given a QModelIndex, I can fetch the internal pointer and cast it to one of my nodes (since that is how I've created that index).

    Acoording to the documentation I have to call beginRemoveRows before touching the underlying data of my model and endRemoveRows after I've changed it. However when I do it like this, the view tries to paint QModelIndexes that are no longer valid for this model. This would not be a problem if I could store a QVariant instead of a void pointer (the problem is that no shared_ptr is holding the node anymore, thefore an access violation occurs).

    I could solve this problem if I simply keep a set of all "valid" nodes in my model but I would really hate to do so.
    This problem occurs in Qt 4.6.1.
    Does anyone have a suggestion on what might be going wrong and if there's a fix for that?

    *edit*
    It seems I have fixed the problem now. Due to this forum I found ModelTest which I applied to my model. Seems that my parent implementation (and others) was quite flawed.
    I wish I had known this test before, it's an awesome tool to find bugs in tree models.
    Last edited by SiS-Shadowman; 12th July 2010 at 10:36. Reason: Problem fixed

Similar Threads

  1. QAbstractItemModel with rowSpan?
    By joeld42 in forum Qt Programming
    Replies: 0
    Last Post: 4th February 2009, 19:53
  2. QAbstractItemModel to different views
    By ganeshshenoy in forum Qt Programming
    Replies: 3
    Last Post: 19th February 2008, 14:43
  3. Once more : QAbstractItemModel woes
    By Valheru in forum Qt Programming
    Replies: 10
    Last Post: 15th January 2008, 11:44
  4. Checkboxes in QAbstractITemModel
    By Valheru in forum Qt Programming
    Replies: 5
    Last Post: 28th November 2007, 21:23
  5. QAbstractItemModel for dummies
    By morty in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2006, 16:25

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.