如何从 qml 启动一个 Qthread? [英] How Start a Qthread from qml?
本文介绍了如何从 qml 启动一个 Qthread?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要立即启动并停止 Qml 文件中的 QThread 扩展类.有什么解决办法吗?这是我的课:
I need to Start immediately and stop then a QThread extended class from Qml File. Is there any solution for that? here is my class :
class SerialManager : public QThread
{
Q_OBJECT
public:
CircularList<unsigned char> buffer_[2];
signals:
void dataReady(short *value,int len,unsigned short sample);
protected:
void run();
};
推荐答案
如果你有这样的 SerialManager:
if you have SerialManager like this:
class SerialManager : public QThread
{
Q_OBJECT
public:
CircularList<unsigned char> buffer_[2];
signals:
void dataReady(short *value,int len,unsigned short sample);
protected:
void run();
};
在main.cpp中添加如下代码:
in main.cpp add bellow code:
qmlRegisterType<SerialManager>("Device",1,0,"Serial");
然后在你的 qml 中这样做:
then in your qml do this:
Component.onCompleted: {
thread.start()
}
Component.onDestruction:
{
thread.quit()
}
这篇关于如何从 qml 启动一个 Qthread?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文