GUI线程检测在Qt库 [英] GUI thread detecting in the Qt library

查看:119
本文介绍了GUI线程检测在Qt库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道我的函数运行在哪个线程的上下文中,它是主GUI线程或一些工作线程。

I need to know in the context of which thread my function is running, is it main GUI thread or some worker thread.

我不能使用简单的解决方案来存储QThread指针在主函数,并将其与QThread :: currentThread(),因为我正在写一个库,我没有访问的主要功能。我当然可以创建InitMyLibary()函数,并要求库用户在GUI线程的上下文中调用它,但我真的对此。

I can't use a simple solution to store QThread pointer in the main function and compare it to QThread::currentThread() because I'm writing a library and I do not have access to the main function. I can of course create InitMyLibary() function and require library user to call it in the context of the GUI thread but I'm really against this.

推荐答案

如果你在lib中有Qt,你可以要求应用程序对象的线程。应用程序对象总是居住在主gui线程中。

If you have Qt in the lib you can ask for the thread of the application object. The application object is alway living in the main gui thread.

void fooWorker()
{
    const bool isGuiThread = 
        QThread::currentThread() == QCoreApplication::instance()->thread();

}

这篇关于GUI线程检测在Qt库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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