自定义 Qobject 的 MovetoThread 问题
MovetoThread problems with custom Qobject
我是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
中执行,您需要使用信号和插槽来触发函数执行。
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- 自定义 Qobject 的 MovetoThread 问题