C++ 线程完成后,如何运行Qt函数? void主窗口::在按钮上点击() { QFuture future=QtConcurrent::run(identify);//Thread1 if(future.isFinished()) { //DoSomething(); } }
我有这个密码。我想在identify函数完成运行后运行C++ 线程完成后,如何运行Qt函数? void主窗口::在按钮上点击() { QFuture future=QtConcurrent::run(identify);//Thread1 if(future.isFinished()) { //DoSomething(); } },c++,multithreading,qt,qtconcurrent,C++,Multithreading,Qt,Qtconcurrent,我有这个密码。我想在identify函数完成运行后运行DoSomething()函数。是否可能?您可以将QFuture对象传递给,并将其finished()信号连接到函数或插槽DoSomething() 例如: void MainWindow::on_pushButton_clicked() { QFuture<int> future = QtConcurrent::run(identify); //Thread1 if (future.isFinished())
DoSomething()
函数。是否可能?您可以将QFuture
对象传递给,并将其finished()
信号连接到函数或插槽DoSomething()
例如:
void MainWindow::on_pushButton_clicked()
{
QFuture<int> future = QtConcurrent::run(identify); //Thread1
if (future.isFinished())
{
//DoSomething();
}
}
void主窗口::在按钮上点击()
{
QFuture future=QtConcurrent::run(identify);//Thread1
QFutureWatcher*watcher=新的QFutureWatcher(此);
连接(观察者,信号(完成()),此,插槽(doSomething());
//完成后也删除观察者
连接(监视程序、信号(完成())、监视程序、插槽(删除稍后());
观察者->设置未来(未来);
}
void MainWindow::DoSomething()//插槽或普通函数
{
// ...
}
或者,您可以使用嵌套的事件循环来保持GUI的响应性,并使所有内容都在同一个函数中:
void MainWindow::on_pushButton_clicked()
{
QFuture<int> future = QtConcurrent::run(identify); //Thread1
QFutureWatcher<int> *watcher = new QFutureWatcher<int>(this);
connect(watcher, SIGNAL(finished()), this, SLOT(doSomething()));
// delete the watcher when finished too
connect(watcher, SIGNAL(finished()), watcher, SLOT(deleteLater()));
watcher->setFuture(future);
}
void MainWindow::DoSomething() // slot or ordinary function
{
// ...
}
void主窗口::在按钮上点击()
{
QFuture future=QtConcurrent::run(identify);//Thread1
未来观察者;
QEventLoop循环;
//如果在循环开始之前发出信号finished(如果调用setFuture时任务已经完成),则需要QueuedConnection
连接(&watcher,信号(finished()),&loop,插槽(quit()),Qt::QueuedConnection);
watcher.setFuture(未来);
loop.exec();
DoSomething();
}
我喜欢这种通话布局
void MainWindow::on_pushButton_clicked()
{
QFuture<int> future = QtConcurrent::run(identify); //Thread1
QFutureWatcher<int> watcher;
QEventLoop loop;
// QueuedConnection is necessary in case the signal finished is emitted before the loop starts (if the task is already finished when setFuture is called)
connect(&watcher, SIGNAL(finished()), &loop, SLOT(quit()), Qt::QueuedConnection);
watcher.setFuture(future);
loop.exec();
DoSomething();
}
auto-watcher=新的QFutureWatcher(此);
连接(观察者和QFutureWatcher::已完成,
[观察者,这]()
{
watcher->deleteLater();
自动恢复=观察者->结果();
// ...
});
watcher->setFuture(QtConcurrent::run(identify,param1,param2));
我已经尝试过了,问题是GUI在线程完成之前一直没有响应。连接(&watcher,SIGNAL(finished()),&myObject,SLOT(handleFinished());我有一个问题,我应该写什么来代替&myObject和handleFinished?@NemethAttila我添加了两个示例。谢谢,第二个示例正是我想要的。@NemethAttila小心本地事件循环。最好是尽可能避免它们。@NemethAttila,因为在处理事件时可能会发生很多事情。例如,您的此
对象可能会被删除。
auto watcher = new QFutureWatcher<int>(this);
connect(watcher, &QFutureWatcher<int>::finished,
[watcher, this] ()
{
watcher->deleteLater();
auto res = watcher->result();
// ...
});
watcher->setFuture(QtConcurrent::run(identify, param1, param2));