QProgressBar update in QThread quitting crashes

2019-08-14 06:13发布

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();
}

1条回答
在下西门庆
2楼-- · 2019-08-14 06:48

This is because of your thread has been deleted. This call deletes the instance of class UsersProcess and QScopedPointer deletes the thread.

connect(this, &UsersProcess::ProcessUserRowsFinished, this, &UsersProcess::deleteLater);

But you also have this connections

connect(this, &UsersProcess::ProcessUserRowsFinished, this->threadRowWorker.data(), &QThread::quit);
connect(this->threadRowWorker.data(), &QThread::finished, this->threadRowWorker.data(), &QThread::deleteLater);

When one of this events fires the thread is already deleted.

查看更多
登录 后发表回答