I have a QT GUI with a QProgressBar. When I clic on a QPushButton, a calculation loop is called from another class. However, my interface is freezing and my QProgressBar is not updated during the calculation process. I tried different examples on internet but I do not succeed to make something work. I provide my test below:
MainWindow.cpp
#include "MainWindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QObject::connect(this->ui.pushbutton, SIGNAL(clicked()), this, SLOT(process()));
}
void MainWindow::process()
{
ListCalc m_Calcul(ui.qprogressbar);
auto thread = new QThread();
m_Calcul->moveToThread(thread);
m_Calcul.process(); // This is the action I would like to put in a thread !
}
MainWindow.h
#include "ListCalc.h"
using namespace std;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
private:
Ui::MainWindowClass ui;
public:
MainWindow(QWidget *parent = Q_NULLPTR);
ListCalc m_Calcul;
private slots:
void process();
};
ListCalc.cpp
#include "ListCalc.h"
ListCalc::ListCalc(QProgressBar* bar) :
m_Bar(bar)
{
}
ListCalc::~ListCalc()
{
}
void ListCalc::process()
{
auto size = 30000;
for (auto i = 0;i < size;++i)
{
m_Bar->setValue(i / size * 100);
std::cout<<"1"<<std::endl;
}
}
ListCalc.h
class ListCalc : public QObject
{
Q_OBJECT
public:
ListCalc(QProgressBar*);
~ListCalc();
private:
QProgressBar* m_Bar;
public slots:
void process();
};
Any idea of how I could do this? If you could give me a simple example on how to change my code to succeed