自定义 Qobject 的 MovetoThread 问题

MovetoThread problems with custom Qobject

本文关键字:问题 MovetoThread Qobject 自定义      更新时间:2023-10-16

我是QThreads的新手。并且不明白为什么我的类没有新的线程 ID。但首先是关于我的问题的一些基本信息。

我有一个普通的c ++类,需要很长时间来计算,让我们命名为HeavyBaseClass。在这个类中,我没有QT元素。

我的目标是在她自己的线程中计算这个类,以便 GUI 在她计算时可以正常运行。

阅读了一些Qt线程问题的实现,我决定将QTthread与QObject一起使用。因为应该可以停止线程,而我第一次尝试QtConcurrent 这是不可能的。

因为我创建了自己的QObject:H.文件:

#include <QObject>
#include <HeavyBaseClass.h>
class Qoptimization : public QObject , HeavyBaseClass
{
    Q_OBJECT
public:
    Qoptimization(HeavyBaseClass INPUT PARAMETER,QObject *parent = 0);
    void debugThread();
signals:
public slots:
};
#endif // QOPTIMIZATION_H

CPP 文件:

#include "qoptimization.h"
#include <QDebug>
#include <QThread>
Qoptimization::Qoptimization(HeavyBaseClass INPUT PARAMETER, QObject *parent) :
    QObject(parent),HeavyBaseClass (INPUT PARAMETER)
{
    qDebug() << " init Thread " <<   this->thread()->currentThreadId() ;
}
void Qoptimization::debugThread(){
    qDebug() << " current Thread " <<   thread()->currentThreadId() ;
}

现在我在主窗口上调用它

qDebug() << " Main Thread " <<   this->thread()->currentThreadId();
Qoptimization qoptimization(f);
QThread *optimizationThread = new QThread();
qDebug() << " before moving to Thread " <<   qoptimization.thread()->currentThreadId();
qoptimization.moveToThread(optimizationThread);
optimizationThread->start();
qoptimization.debugThread();
qDebug() << " after moving Thread " <<   qoptimization.thread()->currentThreadId();

我不明白的问题是我的头 ID 在 alle 输出上是相同的。

调试信息:

 Main Thread  0xd88 
 init Thread  0xd88 
 before moving to Thread  0xd88 
 current Thread  0xd88 
 after moving Thread  0xd88 

你知道我做错了什么吗?


更新:

新CPP

void Qoptimization::debugThread(){
    qDebug() << " in debugThred currentThread() " <<   QThread::currentThread() ;
    qDebug() << " in debugThread Thread " <<   thread() ;
    if ( QThread::currentThread() != thread() )
     {
         // Force slot to be emmited in object thread
         QTimer::singleShot( 0, this, SLOT( debugThread() ) );
         return ;
     }
}

输出:

 Main Thread  0x156c 
 init Thread  0x156c 
 before moving to Thread  0x156c 
 in debugThred currentThread()  QThread(0xeccc00) 
 in debugThread Thread  QThread(0x24cad80) 
 after moving Thread  0x156c 

根据文档,currentThreadId 是 QThread 的 STATIC 成员,因此您始终打印调用者线程 ID。所以你应该打印(quint64)qoptimization.thread()对象的成员。

如果您希望在另一个线程中执行成员void debugThread(); - 您应该将其声明为 SLOT,不要直接调用。您应该通过连接的信号、对象元数据或计时器来调用它。尝试下一个代码:

public slots:
    void Qoptimization::debugThread();
void Qoptimization::debugThread()
{
    if ( QThread::currentThread() != thread() )
    {
        // Force slot to be emmited in object thread
        QTimer::singleShot( 0, this, SLOT( debugThread() ) );
        return ;
    }
    qDebug() << " current Thread " <<   thread()->currentThreadId() ;
}

Qt文档有很好的示例。但首先,您应该了解Qt信号/插槽系统。

------- 用于注释的伪代码:

class MyClass
{
  int thread() const { return m_id; }
  void moveToThread( int threadid ) { m_id = threadid; }
  int m_id;
}
MyClass a;
const int curThread = 1;
a.moveToThread( 2 );
qDebug() << curThread << " " << a.thread();
// output: 1 2 // already different!

当您将对象移动到线程时,这并不意味着调用此对象方法将神奇地在线程中执行。

qoptimization.debugThread();

仍在调用方线程中执行,这就是它应该的样子。要在optimizationThread中执行,您需要使用信号和插槽来触发函数执行。