Results 1 to 6 of 6

Thread: QSqlTableModel and QTableView selection problems

  1. #1
    Join Date
    Oct 2008
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QSqlTableModel and QTableView selection problems

    Hello smart people!

    I have a simple app that displays database rows in a QTableView, using a derived QSqlTableModel. One column of the dataset is editable - a simple checkbox - and to achive this I have redefined data and setData methods with Qt::CheckStateRole.

    All works ok, but there's an annoying problem with the tableView. If I select a row and then call select(), the row selection is cleared model is reset.

    So, if I update the checkbox on one of the rows - this will trigger a select() - my selection is gone and the view whizzes back to the first row!

    Can anyone explain why it's doing this and how to prevent it?

    Thanks!

    Will

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlTableModel and QTableView selection problems

    If you call select() that is basically a reset on the model.
    Thus the selection gets lost.

    You can try to work around that by storing the selection and re-setting it after the select().
    See QAbstractItemView::selectionModel(), QItemSelectionModel::selection(), QItemSelectionModel::select()
    (Always assuming that this works gracefully when the old itemselection contains indexes that no longer exist in the updated model...)

    Please, tell us if that approach works ok.

    HTH

  3. #3
    Join Date
    Oct 2008
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlTableModel and QTableView selection problems

    Thanks for the info - I suspected as much!

    Not only will I need to re-select the item, but also re-position it in the viewport. For that all I should need to know is the first visible row, and then scoll to this after the select statement, using the "QAbstractItemView::PositionAtTop" scroll hint.

    So, the £2,000,000 (about 3 Euros) question is how to find the first visible row?

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlTableModel and QTableView selection problems

    QModelIndex indexAt() gives you the index at a certain point. At QPoint(5,5) you should find the first row.

    I have forgot about the following problem:
    both this index *and* the indexes in the QItemSelection probably will (or at least might) get invalid if you call select.

  5. The following user says thank you to caduel for this useful post:

    montylee (18th February 2009)

  6. #5
    Join Date
    Oct 2008
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlTableModel and QTableView selection problems

    Thanks caduel, your suggestions worked great!

    Just for reference - this is the implementaton:
    Qt Code:
    1. QModelIndex topVisibleRow = tableView->indexAt(QPoint(5,5));
    2.  
    3. m_model->setData(index, 99);
    4.  
    5. tableView->scrollTo(topVisibleRow, QAbstractItemView::PositionAtTop);
    To copy to clipboard, switch view to plain text mode 

    If the row was deleted instead of updated, would the topVisibleRow index still be valid or would it reference the previous row in the model?

    This restores the table position after the triggered select(). I can save a single selected row and re-select that, but there's no way to restore mulitple rows (which may or may not be contiguous), but I can live with that.

    Thanks for your help!

  7. #6
    Join Date
    Aug 2008
    Location
    Nanjing, China
    Posts
    66
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlTableModel and QTableView selection problems

    If the row was deleted instead of updated, would the topVisibleRow index still be valid or would it reference the previous row in the model?
    Before you use the index, you should call isValid().
    I think the index reference the next row,

    By the way, is QTableView has a function named select()? I cannot find it.

    And I have a question:How can I get the index of one column of the tableview which is visible to user? Thanks a lot!
    Jerry

Similar Threads

  1. QTableView, QSqlTableModel - data disappears
    By msh in forum Qt Programming
    Replies: 1
    Last Post: 15th November 2008, 11:50
  2. QTableView and QSqlTableModel problem
    By Misenko in forum Qt Programming
    Replies: 5
    Last Post: 19th August 2008, 21:58
  3. QSqlTableModel and QTableView and sorting
    By JeanC in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2008, 13: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.