除了第一个函数的常量性之外,如果这两个函数之间有任何区别,我正在寻找答案:
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/