如何从 qml 启动一个 Qthread?

2022-01-19 00:00:00 qt qml qthread c++

我需要立即启动并停止 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()

    }

相关文章