问题描述
我似乎无法将 QtConcurrent :: run()
与一个方法(类的函数成员)相关联,只使用一个简单的函数。我如何做到这一点?
I can't seem to be able to associate QtConcurrent::run()
with a method (function member of a class) only with a simple function. How can I do this?
使用常规函数,我不能发出信号及其拖动。为什么会有人认为这是一个更好的替代 QThread
超越我,并希望一些输入。
With a regular function I cannot emit signals and its a drag. Why would anyone find this a better alternative to QThread
is beyond me and would like some input.
推荐答案
是的,这是可能的(而且很容易)。
Yes, this is possible (and quite easy).
下面是一个例子(来自Qt文档):
Here is an example (from the Qt documentation):
// call 'QStringList QString::split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const' in a separate thread
QString string = ...;
QFuture<QStringList> future = QtConcurrent::run(string, &QString::split, QString(", "), QString::KeepEmptyParts, Qt::CaseSensitive);
...
QStringList result = future.result();
基本上,你所要做的就是将一个指针作为第一个参数传递给对象,
Basically, all you have to do is pass a pointer to the object as the first argument and the address of the method as the second argument (followed by any other arguments).
请参阅:
这篇关于是可能使用QtConcurrent :: run()与类的函数成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!