Hello. There is a pool of threads, for example, there are 10 of them. How to make it so that on completion of each of the threads there is an increase in the ProgressBar? For example, there are 10 elements, for each element along the stream, a total of 100/10 each completion of the stream + 10% to progressBar. How to make such a ProgressBar? The test case code is below:

Start of everything:

void Calculate::start() { QVector<int> elements; for (int i = 0; i < 100; i++) { elements.append(i); } QVector<int> KA; for (int i = 0; i < 10; i++) { KA.append(i); } for (int i = 0; i < KA.size(); i++) { Worker * worker = new Worker(i); worker->setAutoDelete(false); worker->setElements(&elements,elements.size()); QThreadPool *threadPool = QThreadPool::globalInstance(); threadPool->setMaxThreadCount(KA.size()); threadPool->start(worker); } } 

Worker.h

 #ifndef WORKER_H #define WORKER_H #include <QObject> #include <QRunnable> class Worker : public QObject, public QRunnable { Q_OBJECT public: Worker(int _indexKA); ~Worker(); void setElements(QVector<int> *_elements, int size) { elements = _elements; sizeElements = size; } virtual void run(); signals: private: int indexKA; QVector<int> *elements; int sizeElements; }; #endif // WORKER_H 

Worker.cpp

 #include "worker.h" #include <QDebug> Worker::Worker(int _indexKA) { indexKA = _indexKA; } Worker::~Worker() { } void Worker::run() { qDebug() << "START"; int summ = 0; for (int i = 0; i < sizeElements; i++) { summ = indexKA + i; qDebug() << summ; } qDebug() << summ; // здесь как то уведомить поток о завершении и передать 10% в прогресс бар } 

    1 answer 1

    Announce the signal of the work of the worker (without parameters), take this signal in the interface from each of the workers of the pool and when a signal arrives, increase the value in the progress bar by a fixed amount calculated from the number of workers. It is understood that the number of workers is known in the place where you want to update the progress bar.

    By itself, a worker must not know how many percent of the work he has completed, since he does not know anything about other workers.

    • I did something like this already, but the signal with the int parameter (100 / number of elements (the same number of threads)) and I link the progress of the bar through the slot where the value is sent and added to the common variable, it is output to the progress bar. - Disastricks
    • If every worker knows how many neighbors he has and can calculate what percentage of the total work he does, then this option is also possible - Bearded Beaver