0

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

froz
  • 163
  • 1
  • 12
  • In Qt, this is the kind of thing you can use a `QTimer` for. It will just get the main Qt thread to do it during it's down time. There's also the possibility of directly calling `QCoreApplication::processEvents` your loop to allow the UI to progress. I'm not sure you want to invest into the effort of threading your `process`. – François Andrieux Jul 17 '19 at 17:46
  • Do you have a minimal example or a link to a minimal example? – froz Jul 17 '19 at 17:46

0 Answers0