Results 1 to 13 of 13

Thread: How to display periodically updated data in QTableView

  1. #1
    Join Date
    Mar 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to display periodically updated data in QTableView

    I am developing an application that updates the data in QTableView with apache server once per second. The server renders the data as XML table. Number of columns is constant, but the number of rows changes each time. The data in the rows may also vary.

    To convert the XML into the data, I created a class TxTableData, which is used in TxTableModel (child of QAbstractTableModel). Also TxTableModel uses QTimer to update data from the server.

    The problem is that if the number of lines decreases - QTableview did not react to it. When the number of rows increases - it's all right.

    Qt Code:
    1. void TxTableModel::refreshData()
    2. {
    3. TxRequest request;
    4. request.setObject("order");
    5. request.setMethod("getlist");
    6. request.addParam("begin_time", 60*60*4);
    7. request.addParam("end_time", 60*4);
    8.  
    9. http.queryAsync(request);
    10. }
    11.  
    12. void TxTableModel::parseXml(const QByteArray &xml)
    13. {
    14. QXmlInputSource inputSource;
    15. TxSaxTableHandler handler(&m_Data, false);
    16.  
    17. inputSource.setData(xml);
    18. reader.setContentHandler(&handler);
    19. reader.setErrorHandler(&handler);
    20.  
    21. beginResetModel();
    22. reader.parse(inputSource);
    23. endResetModel();
    24. }
    25.  
    26. void TxTableModel::httpDone(bool error)
    27. {
    28. if (error) {
    29. qDebug() << http.errorString();
    30. } else {
    31. parseXml(http.readAll());
    32. }
    33. }
    34.  
    35. void TxTableModel::timerDone()
    36. {
    37. refreshData();
    38. }
    To copy to clipboard, switch view to plain text mode 

    How to update the data in QTableview if the number of rows can change?

  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: How to display periodically updated data in QTableView

    What does refreshData() do?
    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 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to display periodically updated data in QTableView


  4. #4
    Join Date
    Mar 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display periodically updated data in QTableView

    Quote Originally Posted by wysota View Post
    What does refreshData() do?
    This method makes async request to apache using QHttp.

    Qt Code:
    1. // QHttp http;
    2.  
    3. TxTableModel::TxTableModel(QObject *parent) :
    4. {
    5. timer = new QTimer(this);
    6.  
    7. connect(&http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
    8. connect(timer, SIGNAL(timeout()), this, SLOT(timerDone()));
    9.  
    10. timer->start(1000);
    11. }
    To copy to clipboard, switch view to plain text mode 

    Read more carefully documentation of QAbstractTableModel and QAbstractItemModel!
    see QAbstractItemModel::beginInsertRows, QAbstractItemModel::beginRemoveColumns and so on
    I get every time a new XML. New and deleted rows can be both at the beginning and at the end or the middle of the document. If I want to use QAbstractItemModel::beginInsertColumns and QAbstractItemModel::beginRemoveRows, should I remove all old rows and then insert all new rows?

    Also, I think I can compare the rows and add or remove the necessary ones without clearing the entire table.

    Is there another way to insert and remove these lines?

    I ask this because I used a similar approach (remove all old rows, add all new rows) in working with wxWidgets. It was very slow, so I decided to switch to Qt.

  5. #5
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to display periodically updated data in QTableView

    If you are lazy and dont care about perfomance you can use QAbstractItemModel::beginResetModel and QAbstractItemModel::endResetModel.
    If you are just changing some values then emit signal: QAbstractItemModel::dataChanged.

  6. #6
    Join Date
    Mar 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display periodically updated data in QTableView

    I am lazy, because it cost 10 ms for 100 rows to delete and insert. I found the answer in a brainstorming session last night.

    My mistake was simple. I have not cleaned the data before I save new.

    This is my code for others:

    Qt Code:
    1. void TxTableModel::parseXml(const QByteArray &xml)
    2. {
    3. QXmlInputSource inputSource;
    4. TxSaxTableHandler handler(&m_Data, false);
    5.  
    6. inputSource.setData(xml);
    7. reader.setContentHandler(&handler);
    8. reader.setErrorHandler(&handler);
    9.  
    10. QModelIndexList selectedList = parent->selectionModel()->selectedRows();
    11.  
    12. beginResetModel();
    13. m_Data.clearData();
    14. reader.parse(inputSource);
    15. endResetModel();
    16.  
    17. for( int i=0; i<selectedList.count(); i++) {
    18. parent->selectRow(selectedList.at(i).row());
    19. }
    20. }
    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: How to display periodically updated data in QTableView

    Just be aware that if you reset the model, you'll lose the selection and positioning in all the views attached to the model.
    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.


  8. #8
    Join Date
    Mar 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display periodically updated data in QTableView

    I can already see it. Is there any way to restore the scroll?

  9. #9
    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: How to display periodically updated data in QTableView

    Don't reset the model
    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.


  10. #10
    Join Date
    Mar 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display periodically updated data in QTableView

    I cant. Line change so often that it is much easier to reset the entire model than to merge

  11. #11
    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: How to display periodically updated data in QTableView

    Quote Originally Posted by nickla View Post
    I cant. Line change so often that it is much easier to reset the entire model than to merge
    Then you won't retain scrolling and selection. Simple as that. It's either easy or effective, the choice is yours.
    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.


  12. #12
    Join Date
    Dec 2009
    Posts
    8
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to display periodically updated data in QTableView

    I had a similar problem and even tried to retain unique data from each selected itens before modelReset and then, after modelReset I tried to search for the data in then new model and finaly select the respective position at the view. Did not work or I was doing something really wrong. I finally surrendered to the model merging process. It is working like a charm but model merging took me a while to figure out!

  13. #13
    Join Date
    Mar 2011
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to display periodically updated data in QTableView

    This is my NonMerge method:

    Qt Code:
    1. void TxTableModel::parseXml(const QByteArray &xml)
    2. {
    3. QXmlInputSource inputSource;
    4. TxSaxTableHandler handler(&m_Data, false);
    5.  
    6. inputSource.setData(xml);
    7. reader.setContentHandler(&handler);
    8. reader.setErrorHandler(&handler);
    9.  
    10. QModelIndexList selectedList = parent->selectionModel()->selectedRows();
    11. QModelIndex scrollTopIndex = parent->indexAt(QPoint(1, 1));
    12. QModelIndex scrollBottomIndex = parent->indexAt(QPoint(1, parent->height() - 1));
    13.  
    14. beginResetModel();
    15. m_Data.clearData();
    16. reader.parse(inputSource);
    17. endResetModel();
    18.  
    19. if (scrollBottomIndex.row() == -1) {
    20. parent->scrollToBottom();
    21. } else {
    22. parent->scrollTo(scrollTopIndex);
    23. }
    24.  
    25. if (selectedList.count() > 0 && (selectedList.at(0).row() >= scrollTopIndex.row()) && (scrollBottomIndex.row() == -1 || scrollBottomIndex.row() > selectedList.at(0).row() )) {
    26. parent->selectRow(selectedList.at(0).row());
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

    But I really hope that you would not use this. It makes me a lot of pain to support (edit cell is something terrible) and I`m working on merge too.

Similar Threads

  1. Display DIB data
    By nightroad in forum Qt Programming
    Replies: 2
    Last Post: 29th December 2010, 21:42
  2. Replies: 1
    Last Post: 23rd September 2010, 20:16
  3. QTableview data display font question
    By MarkoSan in forum Newbie
    Replies: 8
    Last Post: 14th May 2008, 12:57
  4. Replies: 5
    Last Post: 29th January 2008, 17:36

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.