gpt4 book ai didi

c++ - QThread::currentThread () 与 QObject::thread()

转载 作者:太空狗 更新时间:2023-10-29 23:30:02 25 4
gpt4 key购买 nike

除了第一个函数的常量性之外,如果这两个函数之间有任何区别,我正在寻找答案:

QThread * QObject::thread() const
QThread * QThread::currentThread()

最佳答案

它们完全不同。

QThread * QObject::thread() const 返回特定 QObject 所在的线程。

QThread * QThread::currentThread() 返回指向管理当前执行线程的 QThread 的指针。

class MyClass : public QObject
{

};

int main(int argc, char **argv)
{
QApplication app(argc, argv);

MyClass * obj = new MyClass();
QThread thread2;
obj->moveToThread(&thread2);
thread2.start();

qDebug() << "The current thread is " << QThread::currentThread();
qDebug() << "The thread2 address is " << &thread2;
qDebug() << "The object is in thread " << obj->thread();

return app.exec();
}

示例输出:

The current thread is QThread(0x1436b20)
The thread2 address is QThread(0x7fff29753a30)
The object is in thread QThread(0x7fff29753a30)

关于c++ - QThread::currentThread () 与 QObject::thread(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33280672/

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