Results 1 to 7 of 7

Thread: Correctly refreshing a view after a model update.

  1. #1
    Join Date
    Mar 2006
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Correctly refreshing a view after a model update.

    Hello,
    what I want to achieve is the following:
    I have data from a sql database in a subclass of QSqlQueryModel and refresh this model every 10 seconds from the database. The user ideally shouldn´t notice the refresh at all. The view is set so it selects whole rows only, and only one at a time.
    So far, I save the rowID selected in the view (not the QModelIndex but a non-ambiguous value in the table), then refresh the model and afterwards reselect the saved rowID in the view.
    The problem now is that this reselection in the view automatically jumps to the selection, even if the selected row was far down the scrollbar originally. Also, when the project is finished the data in the sql database will grow and new items will automatically be added with the 10 second refresh. What are your suggestions to achieve a refresh with minimal optical impact on the view and hence the user?

    Thanks in advance

  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: Correctly refreshing a view after a model update.

    Did you try using QSqlQueryModel::fetchMore()?

    If that doesn't work, you should inherit the model and provide your own means to refresh it by selecting the data yourself, scanning the result for changes and emitting proper signals (layoutChanged() and dataChanged()) from the model for the view to update itself.

  3. #3
    Join Date
    Apr 2006
    Location
    Minsk, Belarus
    Posts
    33
    Thanks
    2
    Thanked 6 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Correctly refreshing a view after a model update.

    The best way (IMHO) is to inherit model from QAbstractTableModel and apply the following algorithm for this model:
    1) read all desirable items from the database on the start-up and store them in your model
    2) while refreshing model read ONLY newly added items and insert them to model.
    You should use beginInsertRows() and endInsertRows() calls. This automatically emits signals layoutChanged()

    I implemented this schema for my database (rows in this database are just log messages from some external process). Each row has unique ID which is autoincremented so I easily can select only new rows from the database

  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: Correctly refreshing a view after a model update.

    Quote Originally Posted by Conel
    2) while refreshing model read ONLY newly added items and insert them to model.
    And what if one of existing records was updated? or deleted? or you can't distinguish between added later and added earlier? In some cases what you say is possible, but you have to assume many things. Without assumptions you have to reread the whole query.

  5. #5
    Join Date
    Apr 2006
    Location
    Minsk, Belarus
    Posts
    33
    Thanks
    2
    Thanked 6 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Correctly refreshing a view after a model update.

    Yes, this works only with some assumptions. In my case the records could not be modified or deleted

    If database may be changed in any way then the only solution is to fully reread all the table contents.

  6. #6
    Join Date
    Apr 2006
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Correctly refreshing a view after a model update.

    Hi
    I'm trying to update a model that takes the dates from a qprocess output. When it's reading the dates, my application is blocked for a moment and I don't know how to solve it.

    Can you help me?

    Qt Code:
    1. void MainWindow::updateTable()
    2. {
    3. /*Read condor_status -l*/
    4. QModelIndex parent;
    5. int numRow=0;
    6. int maxRow = m_model->rowCount(parent);
    7. QString line;
    8.  
    9. while((line = m_proc->readLine()).size() > 1){
    10. do{
    11. if(numRow >= maxRow){
    12. m_model->insertRows(numRow,1,parent);
    13. maxRow = m_model->rowCount(parent);
    14. }
    15. QString dataNew = line.split(' ', QString::SkipEmptyParts)[2];
    16. QString header = line.split(' ',QString::SkipEmptyParts)[0];
    17. int numColumn = headerData(m_model,header);
    18. QModelIndex index = m_model->index(numRow,numColumn,parent);
    19. QString dataOld = (m_model->data(index)).toString();
    20.  
    21. if(dataOld != dataNew){
    22. m_model->setData(index, dataNew);
    23.  
    24. }
    25. }while((line = m_proc->readLine()).size() > 1);
    26. numRow++;
    27. }
    28.  
    29. QTimer::singleShot(5000, this, SLOT(createProcess()));
    30.  
    31. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    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: Correctly refreshing a view after a model update.

    Do it in a separate thread or use a timer which will fire once in a while and read and process the input which is available at that moment. When the process ends and its data is processed, kill the timer.

Similar Threads

  1. Replies: 1
    Last Post: 18th November 2009, 23:21
  2. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  3. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 08:50
  4. Replies: 9
    Last Post: 7th November 2006, 15:10
  5. Model - View Programming doubt.
    By munna in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2006, 13:01

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.