将c++发出的信号连接到qml连接



我正在从c++发出一个信号,并试图使用qml中的Connections获取值。然而,由于某种未知原因,qml无法识别">OnSomethingHappened",并且c++发出的信号是">somethingHappene

我知道还有其他解决方案,但我需要在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();
}

main.cpp

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类中,但上下文属性myModel对象的类型为QmlMqttClient

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

相关内容

  • 没有找到相关文章

最新更新