QThread::currentThread () 与 QObject::thread()
QThread::currentThread () vs QObject::thread()
除了第一个函数的常量性之外,如果这两个函数之间有任何区别,我正在寻找答案:
QThread * QObject::thread() const
QThread * QThread::currentThread()
它们并不相同,尽管它们可能 return 相同的结果。
第一个 returnQObject 所在的线程。
第二个 return 当前正在执行的线程。
他们做两件不同的事情。
QThread::currentThread()
是一个静态函数,它 returns 指向调用它的线程的指针,即。当前线程。
QObject::thread()
returns 指向该对象所在线程的指针。
他们完全不同。
QThread * QObject::thread() const
returns 特定 QObject
所在的线程。
QThread * QThread::currentThread()
Returns 指向管理当前执行线程的 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)
除了第一个函数的常量性之外,如果这两个函数之间有任何区别,我正在寻找答案:
QThread * QObject::thread() const
QThread * QThread::currentThread()
它们并不相同,尽管它们可能 return 相同的结果。
第一个 returnQObject 所在的线程。
第二个 return 当前正在执行的线程。
他们做两件不同的事情。
QThread::currentThread()
是一个静态函数,它 returns 指向调用它的线程的指针,即。当前线程。
QObject::thread()
returns 指向该对象所在线程的指针。
他们完全不同。
QThread * QObject::thread() const
returns 特定 QObject
所在的线程。
QThread * QThread::currentThread()
Returns 指向管理当前执行线程的 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)