Hello! There is a program that works with a huge amount of data and processes them in several approaches. Please tell me how the user can display the current progress of data processing? If during their processing, it hangs "Not responding". The most effective way that I have found is between each approach:

QApplication::processEvents(); Но при этом, программа может надолго зависнуть. 

I also tried to create a second form to which I sent information through slots / signals. With this approach, the second form also freezes!

  • one
    For example, move the work to another thread from which you send processing progress. There is also a QProgressDialog, look at the example with it, it is very simple and does not require the creation of other threads: doc.qt.io/qt-4.8/qprogressdialog.html#details - gil9red
  • @ gil9red Thanks for the advice, tomorrow I’ll try to work with QProgressDialog, accomplish your goals. Could you tell a little about threads in a few words? - Malice
  • @ gil9red I think QThread is very suitable, if you like, make what you wrote back. I received the necessary information. - Malice

2 answers 2

To solve this problem I decided to use the QThread class. To put it simply, I transfer all the calculations to a separate stream, from which the signals transfer the processing to the GUI. Data Processing Class:

 class worker : public QObject { Q_OBJECT public: explicit worker(QObject *parent = 0); void setup(QThread &wThread); public slots: void someAction(); } 

cpp:

 void worker::setup(QThread &wThread){ connect(&wThread, SIGNAL(started()), this, SLOT(someAction())); } void worker::someAction(){ /// emit progressSignal(int); /// } 

Then I declare a class and transfer its work to another thread:

 int main(int argc, char *argv[]) { QThread cThread; worker cWorker; cWorker.setup(cThread); cWorker.moveToThread(&cThread); cThread.start(); } 

Now the user interface does not freeze and at the same time displays the current progress of work.

  • Upon completion of all calculations, the flow is terminated through the terminate () method; There is also a quit () method; But in truth, I did not understand their difference. - Malice

Use QFuture QFutureWatcher and run the method through QConcurrent::run

  • Try to write more detailed answers. Explain what is the basis of your statement? - Nicolas Chabanovsky