Advertisement
uni

model-view-rows

uni
Feb 7th, 2019
40
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3.  
  4. #include <QDateTime>
  5. #include <QDebug>
  6. #include <QSqlRecord>
  7. #include <QSqlRelationalTableModel>
  8. #include <QThread>
  9. #include <QTimer>
  10.  
  11. MainWindow::MainWindow(QWidget *parent) :
  12.     QMainWindow(parent),
  13.     ui(new Ui::MainWindow)
  14. {
  15.     ui->setupUi(this);
  16.     QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
  17.     db.setDatabaseName("logger.sqlite3");
  18.     if (!db.open()) {
  19.         qFatal("Can't open DB");
  20.     }
  21.     model = new QSqlRelationalTableModel(this, db);
  22.     model->setTable("Logs");
  23.     model->setEditStrategy(QSqlTableModel::OnManualSubmit);
  24.     model->select();
  25.     ui->tableView->setModel(model);
  26.     timer = new QTimer;
  27.     connect(timer, &QTimer::timeout, this, &MainWindow::log);
  28.     timer->start(1000);
  29.     connect(model, &QSqlRelationalTableModel::rowsInserted,
  30.             this, &MainWindow::updateRows,
  31.             Qt::QueuedConnection); //<<< this queued connection
  32. }
  33.  
  34. MainWindow::~MainWindow()
  35. {
  36.     delete ui;
  37. }
  38.  
  39. void MainWindow::log()
  40. {
  41.     qDebug() << "log()" << QThread::currentThread();
  42.     QSqlRecord record = model->record();
  43.     record.setValue(0, QDateTime::currentDateTimeUtc());
  44.     record.setValue(1, 1);
  45.     record.setValue(2, 2);
  46.     record.setValue(3, qrand()%2
  47.                     ? "short string"
  48.                     : "very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string very long string ");
  49.  
  50.     if(model->insertRecord(-1, record)) {
  51.         model->submitAll();
  52.     } else {
  53.         QSqlDatabase::database().rollback();
  54.         qFatal("can't insert");
  55.     }
  56. }
  57.  
  58. void MainWindow::updateRows(const QModelIndex &parent, int first, int last)
  59. {
  60.     qDebug() << "updateRows()" << QThread::currentThread();
  61. //    QTimer::singleShot(1, ui->tableView, &QTableView::resizeRowsToContents);
  62.     ui->tableView->resizeRowsToContents();
  63.     ui->tableView->scrollToBottom();
  64. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement