Results 1 to 2 of 2

Thread: Using QTableView to display an infinite incoming log stream.

  1. #1
    Join Date
    Oct 2016
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Using QTableView to display an infinite incoming log stream.

    Alrighty. As per the title, lets say I receive log messages every so often. In my test, the logs are just a QString time stamp in column 0 and a QString made from an unsigned 64 bit integer in column 1. Column 1's source is a count of the "logs".

    My mainwindow.ui has a single tableView dropped in it.

    Here are my three source files:

    main.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. #include <QApplication>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. MainWindow w;
    9. w.show();
    10. w.loop();
    11.  
    12. return a.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 
    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <thread>
    5.  
    6. #include <QMainWindow>
    7. #include <QStandardItemModel>
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20.  
    21. void loop();
    22.  
    23. private:
    24. Ui::MainWindow *ui;
    25.  
    26. QStandardItemModel *generic_data_model_m;
    27.  
    28. void loop_priv();
    29.  
    30. std::thread *thread_m;
    31.  
    32. bool kill_thread;
    33. };
    34.  
    35. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. #include <QDateTime>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow),
    9. kill_thread(false)
    10. {
    11. generic_data_model_m = new QStandardItemModel(150, 2, this);
    12.  
    13. generic_data_model_m->setHorizontalHeaderItem(0, new QStandardItem(QString("time")));
    14. generic_data_model_m->setHorizontalHeaderItem(1, new QStandardItem(QString("num")));
    15.  
    16. ui->setupUi(this);
    17. ui->tableView->setModel(generic_data_model_m);
    18. generic_data_model_m->setRowCount(0);
    19. }
    20.  
    21. MainWindow::~MainWindow()
    22. {
    23. delete ui;
    24. kill_thread = true;
    25. thread_m->join();
    26. delete thread_m;
    27. delete generic_data_model_m;
    28. }
    29.  
    30. void MainWindow::loop()
    31. {
    32. thread_m = new std::thread(&MainWindow::loop_priv, this);
    33. }
    34.  
    35. void MainWindow::loop_priv()
    36. {
    37. QList<QStandardItem *> items;
    38. quint64 i = 0;
    39.  
    40. while(!kill_thread) {
    41. if (generic_data_model_m->rowCount() > 100) {
    42. std::this_thread::sleep_for(std::chrono::milliseconds(100)); // arbitrary sleep to improve stability
    43. generic_data_model_m->removeRows(0, 50);
    44. std::this_thread::sleep_for(std::chrono::milliseconds(100));
    45. }
    46. QList<QStandardItem *> items;
    47. items = {new QStandardItem(QDateTime::currentDateTime().toString()), new QStandardItem(QString::number(i))};
    48. generic_data_model_m->appendRow(items);
    49. ++i;
    50. }
    51. }
    To copy to clipboard, switch view to plain text mode 

    This is unrealistically fast, but I feel it should still be stable under these conditions. This program is very volatile. Any manipulation of the tableView is likely to cause a crash.

    What I'm simulating is a constant stream of log messages, from some source, being inserted into the item model. The intent is for old logs to be removed as new ones are added.

    Should I approach this with a different method?

    Thanks!

  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: Using QTableView to display an infinite incoming log stream.

    Don't access UI resources from any thread other than the main thread.

    So in your case don't call methods of the table view or the model from the secondary thread.

    Also access to "kill_thread" is neither mutex protected nor is it an atomic.

    Cheers,
    _

Similar Threads

  1. Display webcam stream with phonon and the qt
    By nyquist82 in forum Qt Programming
    Replies: 1
    Last Post: 26th October 2011, 08:03
  2. Display an output buffer (text stream) in QWidget
    By nomiz in forum Qt Programming
    Replies: 8
    Last Post: 30th June 2011, 09:44
  3. Replies: 0
    Last Post: 17th March 2011, 19:38
  4. GUI Freezes unless there's incoming data
    By crisp in forum Qt Programming
    Replies: 17
    Last Post: 7th February 2009, 17:56
  5. QTcpServer limit for incoming connections
    By mdecandia in forum Qt Programming
    Replies: 9
    Last Post: 5th May 2007, 22:09

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.