簡體   English   中英

將從 c++ 發出的信號連接到 qml

[英]connect signal emitted from c++ to qml Connections

我從 c++ 發出一個信號,並嘗試使用 qml 中的連接來獲取值。 然而,代碼正在編譯,由於某些未知原因,qml 無法識別“ OnSomethingHappened ”,並且從 c++ 發出的信號是“ somethingHappened ”。

我知道可以有其他解決方案,但我需要在 qml 中使用連接。 這是因為 qml 中使用的架構。

qmlclient.h

#ifndef QMLMQTTCLIENT_H
#define QMLMQTTCLIENT_H

#include <QtCore/QMap>
#include <QtMqtt/QMqttClient>
#include <QtMqtt/QMqttSubscription>

class QmlMqttClient;

class QmlMqttSubscription : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QMqttTopicFilter topic MEMBER m_topic NOTIFY topicChanged)
public:
    QmlMqttSubscription(QMqttSubscription *s, QmlMqttClient *c);
    ~QmlMqttSubscription();

Q_SIGNALS:
    void topicChanged(QString);
    void messageReceived(QString &msg);
    void somethingHappened(QString text);

public slots:
    void handleMessage(const QMqttMessage &qmsg);

private:
    Q_DISABLE_COPY(QmlMqttSubscription)
    QMqttSubscription *sub;
    QmlMqttClient *client;
    QMqttTopicFilter m_topic;
};

class QmlMqttClient : public QMqttClient
{
    Q_OBJECT
public:
    QmlMqttClient(QObject *parent = nullptr);

    QmlMqttSubscription *subscribe(const QString &topic);

    void sub();
private:
    Q_DISABLE_COPY(QmlMqttClient)
};


#endif // QMLMQTTCLIENT_H*/

qmlmqttclient.cpp

#include "qmlmqttclient.h"
#include <QDebug>

QmlMqttClient::QmlMqttClient(QObject *parent)
    : QMqttClient(parent)
{
}

QmlMqttSubscription* QmlMqttClient::subscribe(const QString &topic)
{
    auto sub = QMqttClient::subscribe(topic, 0);
    auto result = new QmlMqttSubscription(sub, this);
    return result;
}

QmlMqttSubscription::QmlMqttSubscription(QMqttSubscription *s, QmlMqttClient *c)
    : sub(s)
    , client(c)
{
    connect(sub, &QMqttSubscription::messageReceived, this, &QmlMqttSubscription::handleMessage);
   // m_topic = sub->topic();
}

QmlMqttSubscription::~QmlMqttSubscription()
{

}

void QmlMqttSubscription::handleMessage(const QMqttMessage &qmsg)
{
    //emit messageReceived(qmsg.payload());

    emit somethingHappened(qmsg.payload());

    qDebug() << "value -> " + qmsg.payload();
}

主文件

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);


    QmlMqttClient *client =  new QmlMqttClient();
    client->setHostname("127.0.0.1");
    client->setPort(1883);

    client->connectToHost();
    qDebug() << client->state();

    QTimer timer;
    QObject::connect(&timer, &QTimer::timeout, [client]() {client->subscribe("/root/temp");});
    timer.setSingleShot(true);
    timer.start(5000);



    QQmlApplicationEngine engine;

    engine.rootContext()->setContextProperty("myModel",client);

    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    if (engine.rootObjects().isEmpty())
        return -1;

    return app.exec();
}

main.qml

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")

    Rectangle {
        id: sensor
        color: "#ffffff"
        radius: 10
        x: 10
        y: 10

        property string address
        property string title
        property string unit

        Text{
            id: textField

            property var value : 0

            text: sensor.title + " " + value + " " + sensor.unit
            y:50
            font.family: "Helvetica"
            font.pointSize: 24
            anchors.horizontalCenter: parent.horizontalCenter
        }

        Connections
        {
            target: myModel

            onSomethingHappened: {
             console.log(" something happened " +text);
            }
        }
    }
}

在運行上面的代碼控制台顯示

QML debugging is enabled. Only use this in a safe environment.
1
qrc:/main.qml:33:9: QML Connections: Cannot assign to non-existent property "onSomethingHappened"
"value -> -50"
"value -> -50"
"value -> -50"

您嘗試連接的信號位於QmlMqttSubscription class 但您的上下文屬性myModel object 的類型為QmlMqttClient

QmlMqttClient *client =  new QmlMqttClient();
...
engine.rootContext()->setContextProperty("myModel",client);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM