Qt 连接c+发出的信号+;到qml连接 我正在发出一个C++信号,试图用QML中的连接来获取这些值。但是,由于一些未知的原因,QML不能识别出“强>发生/ ,而从C++发出的信号是“发生的< /强>”。
我知道可以有其他解决方案,但我需要在qml中使用连接。这是因为qml中使用了体系结构 qmlclient.hQt 连接c+发出的信号+;到qml连接 我正在发出一个C++信号,试图用QML中的连接来获取这些值。但是,由于一些未知的原因,QML不能识别出“强>发生/ ,而从C++发出的信号是“发生的< /强>”。,qt,qml,qqmlcontext,Qt,Qml,Qqmlcontext,我知道可以有其他解决方案,但我需要在qml中使用连接。这是因为qml中使用了体系结构 qmlclient.h #ifndef QMLMQTTCLIENT_H #define QMLMQTTCLIENT_H #include <QtCore/QMap> #include <QtMqtt/QMqttClient> #include <QtMqtt/QMqttSubscription> class QmlMqttClient; class QmlMqttSub
#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*/
运行上述代码时,控制台显示
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"
您试图连接到的信号位于
qmlmqttcsubscription
类中,但您的上下文属性myModel
对象的类型为QmlMqttClient
QmlMqttClient *client = new QmlMqttClient();
...
engine.rootContext()->setContextProperty("myModel",client);
嗨,谢谢你找到了故障。但我想知道如何从handle消息发出在say QmlMqttClient中声明的信号slot@Mandeep实际上,
emit
只是一个计算结果为零的宏,所以“发出”信号本质上类似于调用任何其他类方法。因此,从理论上讲,您可以“发出”任何类的任何公共方法。但这通常不是一个好的设计,因为它打破了“封装”预期。从你的例子中,我不明白QmlMqttSubscription
是如何在这里发挥作用的(没有看到它被使用),因此很难进一步提出建议。我找到了解决方案。我所做的是在客户机中创建一个插槽,并在收到消息时连接它。只要有来自客户端类的新订阅,就会使用QmlMqttSubscription。我不能使用它们中的任何一个作为基类,因为它们相互使用,并且使用其中一个作为基类可能会导致相互依赖循环错误。这是从为QMqtt提供的qt示例中得到的
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"
QmlMqttClient *client = new QmlMqttClient();
...
engine.rootContext()->setContextProperty("myModel",client);