I have an application in QT with ui. And also I have heavy calculations. When calculations starts, my program stops responding while calculations are running. I wanted to do this via multithreading, but I cannot correctly implement this. I tried to do it with default lib thread:
void HeavyCalculations()
{
// some heavy calculations
}
void MainWindow::on_pushButton_3_clicked()
{
// context is some object in whose context the finished signal should be called, so most likely your MainWindow
auto futureWatcher = new QFutureWatcher<void>(this);
QObject::connect(futureWatcher, &QFutureWatcher<void>::finished, this, compulationFinished);
QObject::connect(futureWatcher, &QFutureWatcher<void>::finished, futureWatcher, &QFutureWatcher<void>::deleteLater);
auto future = QtConcurrent::run( PackBytes );
futureWatcher->setFuture(future);
}
but, where to write th.join()
? If I put this in the next line, it won't make sense. So, do you have any suggestions?
UPD
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_3_clicked();
void compulationFinished();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H