Results 1 to 7 of 7

Thread: QTableModel - beginInsertRows, index not known

  1. #1
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableModel - beginInsertRows, index not known

    Hi,


    I have a model that I derived from a QAbstractTableModel. It provides access to a datasource.

    When I modify a "row" equivalent of the datasource, I only know the index of that "row" until *after* the modification is done. It would be almost as expensive as the actual modification to find out the index of the "row" that is to be modified (and currently the datasource doesnt support that). the problem is of course that I need the row index for the beginInsertRows function.


    I tried it, and it seems to work just fine to first modify the datasource and then call beginInsertRow + endInsertRow. The alternative is to do beginResetModel/endResetModel.


    Do I have to expect any negative side effects? Would you recommend to just reset the model?


    The same problem exists with beginRemoveRows.

  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: QTableModel - beginInsertRows, index not known

    When you modify a row, wouldn't that just require dataChanged()?

    Anyway, if you don't see any bad behavior in your view you should be fine.

    And insert/remove is definitely better than reset.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    tuli (20th August 2015)

  4. #3
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableModel - beginInsertRows, index not known

    Yes, I meant insert a row or delete a row. But great, I'll just leave it like that then.


    Btw, another minor issue: when I emit dataChanged signal, and the model is set to a tablewidget that does not have focus right now, then the data will not be updated in the UI. It will only be updated when I bring the tablewidget to the foreground/focus. Can I change it so it updates right away?

  5. #4
    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: QTableModel - beginInsertRows, index not known

    That is strange, I would have assumed that any cell that is visible will get updated if that cell is part of the range of the dataChanged() signal.

    What do you mean with "foreground/focus" or rather what is the state when it is not that?

    Cheers,
    _

  6. #5
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableModel - beginInsertRows, index not known

    Imagine I have two show()n widgets: one tableview with a model and a separate edit widget, that edits data in the tablview's model and emits dataChanged(). When I have the edit widget on focus, the tableview goes out of focus. Any edit now of a in the background visible talbeview cell will not update the cell. Instead I have to click on the tableview to bring it into focus, and then the cell will update.


    (On windows there is always only one focused/foreground window. Other windows might be visible on the screen, but do not have focus.)

  7. #6
    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: QTableModel - beginInsertRows, index not known

    So the edit widget calls setData() on the model, the model emits dataChanged(), but the view does not update?

    Cheers,
    _

  8. The following user says thank you to anda_skoa for this useful post:

    tuli (22nd August 2015)

  9. #7
    Join Date
    Jul 2012
    Posts
    244
    Thanks
    27
    Thanked 15 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableModel - beginInsertRows, index not known

    Got it! While writing a short example i found the mistake:

    I was passing in index(idx, columnCount()), but that's an invalid index. Should be index(idx, columnCount() - 1). And then it works.

Similar Threads

  1. Replies: 2
    Last Post: 4th January 2014, 02:49
  2. Replies: 1
    Last Post: 6th December 2013, 21:08
  3. Replies: 0
    Last Post: 2nd April 2011, 17:50
  4. Replies: 1
    Last Post: 4th June 2009, 18:44
  5. QTableView QTablemodel & insertRow
    By pfusterschmied in forum Qt Programming
    Replies: 2
    Last Post: 5th June 2007, 13:43

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.