summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaurice Kalinowski <maurice.kalinowski@qt.io>2022-10-18 20:39:32 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-10-23 17:33:55 +0000
commit2b6015f6bf5db7d9bcac0aa4e728e53a97a38401 (patch)
tree9d17b565b222a73c6d0c8a92d08f2ebdd425d2a7
parent3ac7841bdfc705fbbeb6ae5b857be003e066ce94 (diff)
Reimplement QmlMqttClient
QMqttClient is not meant to be directly exported to QML. One reason is that it uses quint16 for port declaration to minimize size of the class. However, that is not properly handled on all platforms for the QML engine. Instead create a proper wrapper for the client and only expose required elements. Fixes: QTBUG-80440 Change-Id: Id4066ce24338b0270af05b06c6216c1755dab533 Reviewed-by: Karsten Heimrich <karsten.heimrich@qt.io> (cherry picked from commit d26a01b1a6f83d05d468baabd13a15866be52502) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--examples/mqtt/quickpublication/qmlmqttclient.cpp53
-rw-r--r--examples/mqtt/quickpublication/qmlmqttclient.h25
-rw-r--r--examples/mqtt/quicksubscription/qmlmqttclient.cpp69
-rw-r--r--examples/mqtt/quicksubscription/qmlmqttclient.h25
4 files changed, 158 insertions, 14 deletions
diff --git a/examples/mqtt/quickpublication/qmlmqttclient.cpp b/examples/mqtt/quickpublication/qmlmqttclient.cpp
index d9c4e94..b4cb3cd 100644
--- a/examples/mqtt/quickpublication/qmlmqttclient.cpp
+++ b/examples/mqtt/quickpublication/qmlmqttclient.cpp
@@ -6,12 +6,61 @@
#include <QMqttTopicName>
QmlMqttClient::QmlMqttClient(QObject *parent)
- : QMqttClient(parent)
+ : QObject(parent)
{
+ connect(&m_client, &QMqttClient::hostnameChanged, this, &QmlMqttClient::hostnameChanged);
+ connect(&m_client, &QMqttClient::portChanged, this, &QmlMqttClient::portChanged);
+ connect(&m_client, &QMqttClient::stateChanged, this, &QmlMqttClient::stateChanged);
+}
+
+void QmlMqttClient::connectToHost()
+{
+ m_client.connectToHost();
+}
+
+void QmlMqttClient::disconnectFromHost()
+{
+ m_client.disconnectFromHost();
+}
+
+const QString QmlMqttClient::hostname() const
+{
+ return m_client.hostname();
+}
+
+void QmlMqttClient::setHostname(const QString &newHostname)
+{
+ m_client.setHostname(newHostname);
+}
+
+int QmlMqttClient::port() const
+{
+ return m_client.port();
+}
+
+void QmlMqttClient::setPort(int newPort)
+{
+ if (newPort < 0 || newPort > std::numeric_limits<quint16>::max()) {
+ qWarning() << "Trying to set invalid port number";
+ return;
+ }
+ m_client.setPort(static_cast<quint16>(newPort));
+
+ m_client.state();
+}
+
+const QMqttClient::ClientState QmlMqttClient::state() const
+{
+ return m_client.state();
+}
+
+void QmlMqttClient::setState(const QMqttClient::ClientState &newState)
+{
+ m_client.setState(newState);
}
int QmlMqttClient::publish(const QString &topic, const QString &message, int qos, bool retain)
{
- auto result = QMqttClient::publish(QMqttTopicName(topic), message.toUtf8(), qos, retain);
+ auto result = m_client.publish(QMqttTopicName(topic), message.toUtf8(), qos, retain);
return result;
}
diff --git a/examples/mqtt/quickpublication/qmlmqttclient.h b/examples/mqtt/quickpublication/qmlmqttclient.h
index bd0f940..8f79229 100644
--- a/examples/mqtt/quickpublication/qmlmqttclient.h
+++ b/examples/mqtt/quickpublication/qmlmqttclient.h
@@ -7,15 +7,38 @@
#include <QtCore/QMap>
#include <QtMqtt/QMqttClient>
-class QmlMqttClient : public QMqttClient
+class QmlMqttClient : public QObject
{
Q_OBJECT
+ Q_PROPERTY(QString hostname READ hostname WRITE setHostname NOTIFY hostnameChanged)
+ Q_PROPERTY(int port READ port WRITE setPort NOTIFY portChanged)
+ Q_PROPERTY(QMqttClient::ClientState state READ state WRITE setState NOTIFY stateChanged)
public:
+
QmlMqttClient(QObject *parent = nullptr);
+ Q_INVOKABLE void connectToHost();
+ Q_INVOKABLE void disconnectFromHost();
Q_INVOKABLE int publish(const QString &topic, const QString &message, int qos = 0, bool retain = false);
+
+ const QString hostname() const;
+ void setHostname(const QString &newHostname);
+
+ int port() const;
+ void setPort(int newPort);
+
+ const QMqttClient::ClientState state() const;
+ void setState(const QMqttClient::ClientState &newState);
+
+signals:
+ void hostnameChanged();
+ void portChanged();
+
+ void stateChanged();
+
private:
Q_DISABLE_COPY(QmlMqttClient)
+ QMqttClient m_client;
};
#endif // QMLMQTTCLIENT_H
diff --git a/examples/mqtt/quicksubscription/qmlmqttclient.cpp b/examples/mqtt/quicksubscription/qmlmqttclient.cpp
index 93fac4f..43b657c 100644
--- a/examples/mqtt/quicksubscription/qmlmqttclient.cpp
+++ b/examples/mqtt/quicksubscription/qmlmqttclient.cpp
@@ -4,31 +4,80 @@
#include "qmlmqttclient.h"
#include <QDebug>
+QmlMqttSubscription::QmlMqttSubscription(QMqttSubscription *s, QmlMqttClient *c)
+ : sub(s)
+ , client(c)
+{
+ connect(sub, &QMqttSubscription::messageReceived, this, &QmlMqttSubscription::handleMessage);
+ m_topic = sub->topic();
+}
+
+QmlMqttSubscription::~QmlMqttSubscription()
+{
+}
+
QmlMqttClient::QmlMqttClient(QObject *parent)
- : QMqttClient(parent)
+ : QObject(parent)
+{
+ connect(&m_client, &QMqttClient::hostnameChanged, this, &QmlMqttClient::hostnameChanged);
+ connect(&m_client, &QMqttClient::portChanged, this, &QmlMqttClient::portChanged);
+ connect(&m_client, &QMqttClient::stateChanged, this, &QmlMqttClient::stateChanged);
+}
+
+void QmlMqttClient::connectToHost()
+{
+ m_client.connectToHost();
+}
+
+void QmlMqttClient::disconnectFromHost()
{
+ m_client.disconnectFromHost();
}
QmlMqttSubscription* QmlMqttClient::subscribe(const QString &topic)
{
- auto sub = QMqttClient::subscribe(topic, 0);
+ auto sub = m_client.subscribe(topic, 0);
auto result = new QmlMqttSubscription(sub, this);
return result;
}
-QmlMqttSubscription::QmlMqttSubscription(QMqttSubscription *s, QmlMqttClient *c)
- : sub(s)
- , client(c)
+void QmlMqttSubscription::handleMessage(const QMqttMessage &qmsg)
{
- connect(sub, &QMqttSubscription::messageReceived, this, &QmlMqttSubscription::handleMessage);
- m_topic = sub->topic();
+ emit messageReceived(qmsg.payload());
}
-QmlMqttSubscription::~QmlMqttSubscription()
+const QString QmlMqttClient::hostname() const
{
+ return m_client.hostname();
}
-void QmlMqttSubscription::handleMessage(const QMqttMessage &qmsg)
+void QmlMqttClient::setHostname(const QString &newHostname)
{
- emit messageReceived(qmsg.payload());
+ m_client.setHostname(newHostname);
+}
+
+int QmlMqttClient::port() const
+{
+ return m_client.port();
+}
+
+void QmlMqttClient::setPort(int newPort)
+{
+ if (newPort < 0 || newPort > std::numeric_limits<quint16>::max()) {
+ qWarning() << "Trying to set invalid port number";
+ return;
+ }
+ m_client.setPort(static_cast<quint16>(newPort));
+
+ m_client.state();
+}
+
+const QMqttClient::ClientState QmlMqttClient::state() const
+{
+ return m_client.state();
+}
+
+void QmlMqttClient::setState(const QMqttClient::ClientState &newState)
+{
+ m_client.setState(newState);
}
diff --git a/examples/mqtt/quicksubscription/qmlmqttclient.h b/examples/mqtt/quicksubscription/qmlmqttclient.h
index 26ee80b..e229288 100644
--- a/examples/mqtt/quicksubscription/qmlmqttclient.h
+++ b/examples/mqtt/quicksubscription/qmlmqttclient.h
@@ -32,15 +32,38 @@ private:
QMqttTopicFilter m_topic;
};
-class QmlMqttClient : public QMqttClient
+class QmlMqttClient : public QObject
{
Q_OBJECT
+ Q_PROPERTY(QString hostname READ hostname WRITE setHostname NOTIFY hostnameChanged)
+ Q_PROPERTY(int port READ port WRITE setPort NOTIFY portChanged)
+ Q_PROPERTY(QMqttClient::ClientState state READ state WRITE setState NOTIFY stateChanged)
public:
+
QmlMqttClient(QObject *parent = nullptr);
+ Q_INVOKABLE void connectToHost();
+ Q_INVOKABLE void disconnectFromHost();
Q_INVOKABLE QmlMqttSubscription *subscribe(const QString &topic);
+
+ const QString hostname() const;
+ void setHostname(const QString &newHostname);
+
+ int port() const;
+ void setPort(int newPort);
+
+ const QMqttClient::ClientState state() const;
+ void setState(const QMqttClient::ClientState &newState);
+
+signals:
+ void hostnameChanged();
+ void portChanged();
+
+ void stateChanged();
+
private:
Q_DISABLE_COPY(QmlMqttClient)
+ QMqttClient m_client;
};
#endif // QMLMQTTCLIENT_H