How can I modify software for multi-threading.

I would like to see the minimum basic example in order to figure it out and not get into the wilds for now.

There is a big table (100 thousand names) at their request the GUI hangs. I would like to eliminate it.

The logic was like that.

  1. The man opens the program.
  2. The form constructor sent the request to the thread in the database.
  3. Built framework application.
  4. We take data from the stream to the main one.
  5. Fill in the received data table.

ps I tried to do this on signals and slots using QThread. I sent a request to another thread, but it started working only when the main application closes ...

enter image description here

mainwindow.CPP

#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { //QSqlDatabase objDatabase; objDatabase = QSqlDatabase::addDatabase("QMYSQL"); objDatabase.setDatabaseName("librarydb"); objDatabase.setHostName("127.0.0.1"); objDatabase.setPort(3306); objDatabase.setUserName("hays0503"); objDatabase.setPassword("hays0503"); objDatabase.open(); //QSqlTableModel *objTableModel; objTableModel = new QSqlTableModel(); objTableModel->setTable("author"); if (!objTableModel->select()) { qDebug()<<"Error"; }else { ui->tableView->setModel(objTableModel); } } 

mainwindow.H

 #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QDebug> #include <QSql> #include <QSqlError> #include <QSqlDatabase> #include <QSqlTableModel> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_pushButton_clicked(); private: Ui::MainWindow *ui; QSqlDatabase objDatabase; QSqlTableModel *objTableModel; }; #endif // MAINWINDOW_H 

    1 answer 1

     void MainWindow::on_pushButton_clicked() { QFuture<QSqlTableModel*> future = QtConcurrent::run( [this]() { // Исполняем этот код в другом потоке //QSqlDatabase objDatabase; objDatabase = QSqlDatabase::addDatabase("QMYSQL"); objDatabase.setDatabaseName("librarydb"); objDatabase.setHostName("127.0.0.1"); objDatabase.setPort(3306); objDatabase.setUserName("hays0503"); objDatabase.setPassword("hays0503"); objDatabase.open(); //QSqlTableModel *objTableModel; objTableModel = new QSqlTableModel(this); objTableModel->setTable("author"); if (!objTableModel->select()) { delete objTableModel; objTableModel = nullptr; qDebug()<<"Error"; } return objTableModel; }); QFutureWatcher<QSqlTableModel*> *watcher = new QFutureWatcher<QSqlTableModel*>(this); connect(watcher, SIGNAL(finished()), watcher, [this, watcher]() { // Запускаем этот код в UI потоке, когда объект future завершит свою работу в рабочем потоке ui->tableView->setModel(watcher->result()); watcher->deleteLater(); // Удалим ненужный watcher }, Qt::QueuedConnection); ); watcher->setFuture(future); // Связываем watcher с feature. Это быстрая операция и не тормозит поток UI } 

    If you click on a button many times, many identical tasks will be launched at once. We must not forget that you can work with objDatabase and objTableModel objects only from one thread at a time.

    • Yes, I heard about this Qt-All SQL Architektura works strictly on one thread (where it was created there and work) and a big restriction on manipulating data. - hays
    • I caught an error when connecting a signal \ slots to a lambda expression. Code: pastebin.com/0Gq8mpj0 CodeError: pastebin.com/tF3pngt4 . IMG: ibb.co/VW0LmtX - hays