Results 1 to 4 of 4

Thread: Qt AbstractItemModel removeRows and delete causing core

  1. #1
    Join Date
    Jul 2013
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Qt AbstractItemModel removeRows and delete causing core

    I have a custom AbstractItemModel implementation with the following to insert nodes

    Qt Code:
    1. layoutAboutToBeChanged();
    2. beginInsertRows(createIndex(p_parent->row(), 0, p_parent), start, end);
    3. TreeNode* p_node = new TreeNode(p_parent, p_data);
    4. p_parent->appendChild(start, p_node);
    5. endInsertRows();
    6. layoutChanged();
    To copy to clipboard, switch view to plain text mode 

    And to remove rows:

    Qt Code:
    1. layoutAboutToBeChanged();
    2. beginRemoveRows(createIndex(p_parent->row(), 0, p_parent), row, row);
    3. p_parent->removeChildren(row, row+1, this);
    4. endRemoveRows();
    5. layoutChanged();
    To copy to clipboard, switch view to plain text mode 

    When removeChildren is called, for each node that is removed the following is done:

    Qt Code:
    1. changePersistentIndex(createIndex(p_node->row(), 0, p_node), QModelIndex());
    2. delete p_node;
    To copy to clipboard, switch view to plain text mode 

    This works. I can add nodes and remove nodes.

    Terminology NOTE: I'm using "nodes" and "rows" interchangeably. Sorry for any confusion.

    What doesn't work:

    1. If a new row is inserted in front of a selected node. The newly
    inserted node becomes selected. [This is not what I expect of want.]
    2. If a row is selected and then later deleted, immediate core dump.
    3. If mouse over a row that is deleted, immediate core dump.

    If I don't delete p_node. Everything runs fine. But obviously that creates a memory leak.
    What am I doing wrong?

    For reference I'm using QT 5.0.2 on 64 bit linux.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt AbstractItemModel removeRows and delete causing core

    Have you tried without the layout change calls? That is usually not necessary when adding/removing rows or columns.

    Cheers,
    _

  3. #3
    Join Date
    Jul 2013
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt AbstractItemModel removeRows and delete causing core

    If I don't make the calls to the layout functions the gui tree doesn't show changes to the model.

  4. #4
    Join Date
    Jul 2013
    Posts
    3
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt AbstractItemModel removeRows and delete causing core

    Bump.

    I'd really like to solve this problem.

    I don't know what I'm doing wrong.

    The only way I've been able to create a working solution is to use QStandardItemModel and QStandardItem. But that item based approach is not suited to my problem and working that way is unnatural and just wrong. My data is row based NOT item based. My number of columns will never change. A cell of data (an item) by itself simply cannot exist in my application.

    I've tried in 2 different custom implementations to just call beingInsertRows and endInsertRows but it never works (no rows are added or removed from the view). It only works when I call the layoutchange methods. And I read somewhere (stackoverflow I think) that I need to update the persistent indexes when I remove rows. So I'm doing that as well.

    I feel like I'm doing so much work just to add/remove a row. Shouldn't the persisted indexes update on their own? Shouldn't the selected row remain selected when I insert a new row, rather than me having to do additional work to update yet another set of indexes somewhere?

Similar Threads

  1. Simple removeRows isn't working...
    By scott_hollen in forum Qt Programming
    Replies: 3
    Last Post: 25th May 2011, 01:22
  2. Qt 4.7 SSE2 causing my app to crash
    By jonks in forum Qt Programming
    Replies: 8
    Last Post: 26th September 2010, 18:53
  3. removeRows in a QTreeView
    By mhoover in forum Qt Programming
    Replies: 2
    Last Post: 1st September 2009, 00:37
  4. Replies: 4
    Last Post: 19th February 2009, 11:10
  5. RemoveRows
    By Untersander in forum Qt Programming
    Replies: 6
    Last Post: 11th January 2006, 09:44

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
  •  
Qt is a trademark of The Qt Company.