我开发了一个带有接口的qt程序。我还有一个复杂的计算,它是在独立于ui线程的线程上完成的。我想从执行计算的线程中更新progressBar。但我得到一个错误,无法更改属于另一个线程的对象。
这是我的代码:
void Somefunc()
{
ui->progressBar->setValue(progress);
}
void MainWindow::on_pushButton_3_clicked()
{
auto futureWatcher = new QFutureWatcher<void>(this);
QObject::connect(futureWatcher, &QFutureWatcher<void>::finished, futureWatcher, &QFutureWatcher<void>::deleteLater);
auto future = QtConcurrent::run( [=]{ SomeFunc(); });
futureWatcher->setFuture(future);
}如何正确地更新进度条?
发布于 2022-04-14 18:04:06
使用信号/时隙组合,特别是排队连接类型(Qt::ConnectionType)。因此,按照这样的思路:
void MainWindow::Somefunc()
{
emit computationProgress(progress);
}
void MainWindow::setProgress(int progress)
{
ui->progressBar->setValue(progress);
}
void MainWindow::on_pushButton_3_clicked()
{
auto futureWatcher = new QFutureWatcher<void>(this);
connect(futureWatcher, &QFutureWatcher<void>::finished, futureWatcher, &QFutureWatcher<void>::deleteLater);
auto future = QtConcurrent::run( [=]{ SomeFunc(); });
futureWatcher->setFuture(future);
connect(this, &MainWindow::computationProgress, this, &MainWindow::setProgress, Qt::QueuedConnection);
}https://stackoverflow.com/questions/71875808
复制相似问题