Results 1 to 2 of 2

Thread: Deleteting/Inserting rows in QAbstractItemModel

  1. #1
    Join Date
    Mar 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Deleteting/Inserting rows in QAbstractItemModel

    I have a tree view that displays data from an external source which I have enclosed in a class. This data is dynamic in nature. I also subclassed a QAbstractItemModel and I am now able to display stuff initially with no problem. Having the dynamic nature of the data, I'm having trouble with how I would be able to update the QTreeView display without reloading everything. I read the documentation and some of the code samples but I find that implementing insertRows() and the other stuff somewhat obscure and have sought to use other means instead since I've read I don't have to implement them in order to achieve what I want.

    insertRow()/removeRow() is implemented by QAbstractItemModel and is not virtual, whereas insertRows() is. I want to use that instead since I find it more straightforward. How would the QAbstractItemModel remove a 'row' in my model class if it has no idea how I implemented it? I imagine this to be possible if there were functions that deleted something at all by me but there isn't. Also, assuming that this would work, how would then I remove a row in the view? Do I have to manually call the functions of the view to delete a displayed row? Or do they sort of sync?

    What are the other means to achieve this dynamic tree view? How would I use insertRow() in a subclassed abstract model? (note: no 's')

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Deleteting/Inserting rows in QAbstractItemModel

    QAbtractItemModel::removeRow() is a wrapper around the more general, and virtual, removeRows(). If there is a functional removeRows() in your subclass then insertRow() will function because the virtual function will be called. The same logic holds for insertRow()/insertRows() and the column equivalents.

    If your removeRows() implementation correctly emits the beginRemoveRows() and endRemoveRows() signals then attached views will correctly update.

    The whole exercise is covered in much detail in the Model subclassing reference as part of the broader picture.

Similar Threads

  1. Simple DOM Model - making it editable - inserting rows
    By Wolf900 in forum Qt Programming
    Replies: 5
    Last Post: 1st March 2012, 10:18
  2. Replies: 1
    Last Post: 7th September 2011, 18:04
  3. Efficient way of inserting rows?
    By afflictedd2 in forum Qt Programming
    Replies: 1
    Last Post: 14th July 2008, 20:01
  4. Treeview scrollbar not updating when inserting rows
    By Khal Drogo in forum Qt Programming
    Replies: 11
    Last Post: 29th November 2007, 13:13
  5. Problem inserting child items into a QAbstractItemModel
    By Valheru in forum Qt Programming
    Replies: 5
    Last Post: 14th October 2006, 18:35

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.