I have the following code that works as expected. It updates the progress bar value in each for loop. The only issue I have is when I'm done and call emit ProcessUserRowsFinished() in the method OnProcessUserRowsStarted the program crashes.
class UsersProcess: public QObject
{
Q_OBJECT
public:
UsersProcess(CTCore::DBConnect* db_context, UserSettingsMap user_settings_map);
void SetProgressBar(QProgressBar *progress_bar);
private:
QProgressBar *progressBar;
QSharedPointer<QList<CTCoreZen::User>> listUsers;
QScopedPointer<QThread> threadRowWorker;
signals:
void ProcessUserRowsFinished();
void ProgressBarSetValue(int value);
}
void UsersProcess::SetProgressBar(QProgressBar *progress_bar)
{
this->progressBar = progress_bar;
}
void UsersProcess::OnUserListSuccess(QList<CTCoreZen::User> *users)
{
this->listUsers.reset(users);
this->progressBar->setVisible(true);
this->progressBar->setTextVisible(true);
this->progressBar->setMinimum(0);
this->progressBar->setMaximum(this->listUsers->size());
this->progressBar->setValue(0);
this->threadRowWorker.reset(new QThread());
this->moveToThread(this->threadRowWorker.data());
connect(this->threadRowWorker.data(), &QThread::started, this, &UsersProcess::OnProcessUserRowsStarted);
connect(this, &UsersProcess::ProgressBarSetValue, this->progressBar, &QProgressBar::setValue);
connect(this, &UsersProcess::ProcessUserRowsFinished, this->threadRowWorker.data(), &QThread::quit);
connect(this, &UsersProcess::ProcessUserRowsFinished, this, &UsersProcess::deleteLater);
connect(this->threadRowWorker.data(), &QThread::finished, this->threadRowWorker.data(), &QThread::deleteLater);
this->threadRowWorker->start();
}
void UsersProcess::OnProcessUserRowsStarted()
{
int row = 0;
UsersData userData(this->dbContext);
int maxRows = this->listUsers->size();
for(auto iter = this->listUsers->begin(); iter != this->listUsers->end(); ++iter)
{
row++;
emit this->ProgressBarSetValue(row);
}
emit ProcessUserRowsFinished();
}
This is because of your thread has been deleted. This call deletes the instance of class UsersProcess and QScopedPointer deletes the thread.
But you also have this connections
When one of this events fires the thread is already deleted.