如何使用带有重载函数的QtConcurrent :: run [英] How to use QtConcurrent::run with overloaded Function

查看:433
本文介绍了如何使用带有重载函数的QtConcurrent :: run的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在尝试并行化我的代码,因此我正在使用QtConcurrent::run,问题是,运行不知道要选择哪个函数.

I'm currently trying to parallelize my code, therefore I'm using QtConcurrent::run and the problem is, run doesn't know which function to choose.

是否可以使用带有重载功能的运行,还是可以找到某种解决方法?

Is there a way to use run with an overloaded function or do I have find some sort of workaround?

推荐答案

您只需static_cast指针以确保过程中没有歧义

You can just static_cast the pointer to ensure there's no ambiguity in the process

void hello(QString name)
{
    qDebug() << "Hello" << name << "from" << QThread::currentThread();
}

void hello(int age)
{
    qDebug() << "Hello" << age << "from" << QThread::currentThread();
}

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    QFuture<void> f1 = run(static_cast<void(*)(QString)>(hello), QString("Alice"));
    QFuture<void> f2 = run(static_cast<void(*)(int)>(hello), 42);
    f1.waitForFinished();
    f2.waitForFinished();
}

或者获取指向正确指针的指针.

or alternatively get a pointer to the right one.

这篇关于如何使用带有重载函数的QtConcurrent :: run的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆