gpt4 book ai didi

c++ - 如何通过 QtConcurrent 调用带参数的函数

转载 作者:行者123 更新时间:2023-11-30 00:50:49 32 4
gpt4 key购买 nike

<分区>

在我的应用程序中,有一个保存过程阻塞了我的 UI。优化还不够,所以我想用 QtConcurrent 实现线程,但我无法让它工作,尽管语法看起来很简单。

这就是现在的样子(原理):

Traymenu::Traymenu(QApplication *a){
//...

void save(){
//Get the savepath, choose if saving is necessary, and trigger saving
QString path = getSavePath();
saveNotes(path);
}

void saveNotes(QString savePath){
//save to the given path, takes quite a while, should be parallel
}
}

我尝试过的:

Traymenu::Traymenu(QApplication *a){
//...

void save(){
//Get the savepath, choose if saving is necessary, and trigger saving
QString path = getSavePath();
QFuture<void> result = QtConcurrent(saveNotes, path); //ERROR
}

void saveNotes(QString savePath){
//save to the given path
}
}

此外,在我的标题中,我包括了这个:

#include <QtConcurrent/QtConcurrentRun>

错误信息是

C:\App\appname\traymenu.cpp:584: Error: no matching function for call 
to 'run(<unresolved overloaded function type>, QString&)'
QFuture<void> future = QtConcurrent::run(save, outputFilename);
^

我也试过这个:

QFuture<void> future = QtConcurrent::run(this, this->save(outputFilename));

这里有错误

C:\App\appname\traymenu.cpp:584: Error: invalid use of void expression
QFuture<void> future = QtConcurrent::run(this, this->save(outputFilename));
^

我的标题看起来像:

class Traymenu : public QSystemTrayIcon
{
Q_OBJECT
public:
Traymenu(QApplication *);
~Traymenu();

void save();
void saveNotes(QString);
//[...]

我做错了什么?

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com