Results 1 to 4 of 4

Thread: beginInsertRows() beginRemoveRows()

  1. #1
    Join Date
    Nov 2008
    Posts
    183
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default beginInsertRows() beginRemoveRows()

    This probably sounds like a simpleton question but...

    Every example I have seen, including the abstractitemmodel which is included with QtCreator does the following:

    beginInsertRows(QModelIndex(), rowCount(), rowCount());

    On a very first call I guess a QModelIndex does have to be created, however, the example repeatedly calls this method:

    model.addAnimal(Animal("Wolf", "Medium"));
    model.addAnimal(Animal("Polar bear", "Large"));
    model.addAnimal(Animal("Quoll", "Small"));


    I'm asking because, while it "seems" to work, it 'duth maketh no senseth'. Why? Because, it would seem to me, one should be using the same index between the view and the model.

    I'm asking because I'm currently writing an application which "periscopes" over a large database showing only a few rows at a time in the view. I am adding and deleting rows in my model all of the time. It would seem creating a new QModelIndex() each time I insert/remove rows should send things into a stack dump situation at some point.

    Are all of these examples just really bad or am I missing something in the documentation?

    Thanks,

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: beginInsertRows() beginRemoveRows()

    I don't see the relation between beginInsertRows and your question. QModelIndex is a short lived object that can be repeatedly created and destroyed. Objects of this type are not stored anywhere and especially not in the model. If storing an index is required for some reason (e.g. to store the currently selected index in a view) then QPersistentModelIndex is used instead but you should avoid is use of possible as it is much heavier than its light cousin.
    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. #3
    Join Date
    Nov 2008
    Posts
    183
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: beginInsertRows() beginRemoveRows()

    Thank you.

    The piece of information I was missing was the fact it is a short lived entity. For some reason I believed it was going to be the index for the entire model, not just the handful of rows I might be adding to the end of the model.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    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: beginInsertRows() beginRemoveRows()

    The model index can be treated as an iterator or even a volatile pointer on an item in the model. It is not tied in any way with the actual physical data in the model. You are passing QModelIndex() to beginInsertRows() to note that you are adding nodes to top-most level of hierarchy in the model. If you wanted to add a child item to one of the existing items, you would first have to get an index of that existing item using QAbstractItemModel::index() and then pass that index to beginInsertRows().
    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.


Similar Threads

  1. QModelIndex validity during beginRemoveRows
    By jkv in forum Qt Programming
    Replies: 4
    Last Post: 19th November 2010, 11:02
  2. Replies: 1
    Last Post: 23rd September 2010, 05:45
  3. QAbstractItemModel::beginRemoveRows/endRemoveRows
    By SiS-Shadowman in forum Qt Programming
    Replies: 0
    Last Post: 12th July 2010, 08:56
  4. beginInsertRows seems not to be working
    By ugurkoltuk in forum Qt Programming
    Replies: 3
    Last Post: 6th January 2010, 14:21
  5. beginInsertRows was not declared ???
    By travlr in forum Qt Programming
    Replies: 2
    Last Post: 6th June 2009, 21:22

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.