Results 1 to 10 of 10

Thread: QTableView : How to refresh the view after an update of the model ?

  1. #1
    Join Date
    Jun 2006
    Posts
    18
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QTableView : How to refresh the view after an update of the model ?

    Hi,

    I have :
    QTableView *PersonTableView;
    int m_PersonLine;

    with these instructions, I can update the name of the person :
    QAbstractItemModel *aim = PersonTableView->model();
    aim->setData(aim->index(m_PersonLine, 1), sr.field("name").value().toString());
    aim->submit();

    Because I do not see the modification on the PersonTableView, I have to quit the application and restart it to view the modification of the name of the person in the PersonTableView.

    Can someone post the code to tell to the PersonTableView that his model has changed and that it has to repaint or refresh or update or something else, the view.
    Please post the code and not only the theory, because I am a newbie.

    Regards.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableView : How to refresh the view after an update of the model ?

    What kind of model is this? Your own? If so, you should emit dataChanged() signal from within setData().

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

    marvaneke (26th July 2006)

  4. #3
    Join Date
    Jun 2006
    Posts
    18
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView : How to refresh the view after an update of the model ?

    Thanks wysota for the (too ) quick answer.

    The model of the PersonTableView is the code below :
    model->setTable("person");
    model->setEditStrategy(QSqlTableModel::OnRowChange);
    model->setSort(1, Qt::AscendingOrder);
    model->setSort(2, Qt::AscendingOrder);
    model->select();
    PersonTableView->setModel(model);

    I have tried the code below :
    aim->dataChanged(aim->index(m_PersonLine, 0), aim->index(m_PersonLine, 2));
    But the compiler answer me that it was "protected".

    Regards.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableView : How to refresh the view after an update of the model ?

    You can always do model->select() after model->setData(), but it should be done automatically.

  6. The following 3 users say thank you to wysota for this useful post:

    marvaneke (27th July 2006), milli (9th May 2011), omid_kma (13th April 2013)

  7. #5
    Join Date
    Jun 2006
    Posts
    18
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView : How to refresh the view after an update of the model ?

    Hi,

    with the instruction :
    PersonTableView->model()->select();
    I receive the error :
    error: ‘class QAbstractItemModel’ has no member named ‘select’

    Have an idea ?

    Regards.

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableView : How to refresh the view after an update of the model ?

    Sure

    Qt Code:
    1. QSqlQueryModel *qmodel = dynamic_cast<QSqlQueryModel*>(PersonTableView->model());
    2. if(qmodel) qmodel->select();
    To copy to clipboard, switch view to plain text mode 

    Alternatively use qobject_cast instead of dynamic_cast if your compiler doesn't support dynamic casting.

  9. The following user says thank you to wysota for this useful post:

    marvaneke (27th July 2006)

  10. #7
    Join Date
    Jun 2006
    Posts
    18
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Re: QTableView : How to refresh the view after an update of the model ?

    Hi wysota,

    With QSqlQueryModel, it does not work, but with QSqlTableModel, it works. (As you see, with your advice, I learn. )
    This line of code is working:
    QSqlTableModel *stm = dynamic_cast<QSqlTableModel*>(PersonTableView->model());
    Or this line of code is also working:
    QSqlTableModel *stm = qobject_cast<QSqlTableModel*>(PersonTableView->model());
    The next line is working :
    if(stm) stm->select();

    But, it as no effect. I need always to quit the application and to start it again to view the modification of the data showing in the TableView.

    Have I to specify something on the View to refresh it, or something like that ?

    Regards.

  11. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableView : How to refresh the view after an update of the model ?

    No, it should get refreshed by itself. But as a last resort (although I think there might be a problem with some other part of your code or with the dbms), try QAbstractItemModel (just remember it is protected). Does the dbms actually get updated when you edit the item? Please note that with the edit strategy you chose, the dbms will get updated only when you change the current row in the view.

  12. The following user says thank you to wysota for this useful post:

    marvaneke (28th July 2006)

  13. #9
    Join Date
    Nov 2006
    Location
    Dresden, Germany
    Posts
    95
    Thanks
    9
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView : How to refresh the view after an update of the model ?

    I simply set the model again in table view using setModel(). Don't know whether this is most efficient but it works...

  14. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableView : How to refresh the view after an update of the model ?

    Quote Originally Posted by ghorwin View Post
    I simply set the model again in table view using setModel(). Don't know whether this is most efficient but it works...
    That's the worst you can do, as all the model-view framework is then reset. It's even worse than if you would call reset() on the model - the result would be the same but without the need to reinstall the model into the view (just the data would be refetched). The proper solution is to emit layoutChanged() or dataChanged() from the model.

Similar Threads

  1. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 21:17
  2. Model - View Programming doubt.
    By munna in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2006, 14:01
  3. Replies: 6
    Last Post: 20th April 2006, 11:23
  4. Model, view, resize to show all data
    By jcr in forum Qt Programming
    Replies: 2
    Last Post: 17th April 2006, 21:59
  5. Table model / view editing issue
    By Caius Aérobus in forum Qt Programming
    Replies: 9
    Last Post: 7th April 2006, 12:03

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.