简体   繁体   中英

GUI thread detecting in the Qt library

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

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.

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();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM