QProgressBar update in QThread quitting crashes

混江龙づ霸主 提交于 2020-01-25 02:35:10

问题


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:


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.



来源:https://stackoverflow.com/questions/34504282/qprogressbar-update-in-qthread-quitting-crashes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!