ex07a_01
ex07a_01.pro
QT += quick mqtt
// ...
main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
width: 640
height: 480
visible: true
title: qsTr("ex07a - v0.1")
EstatMqtt{
anchors.centerIn: parent
}
}
EstatMqtt.qml
import QtQuick 2.4
EstatMqttForm {
}
EstatMqttForm.ui.qml
import QtQuick 2.4
import QtQuick.Controls 2.15
Item {
id: item1
width: 400
height: 400
property alias btConnecta: btConnecta
property alias textInfo: textInfo
Text {
id: textInfo
y: 56
height: 53
color: "#ee2222"
text: qsTr("MQTT desconnectat")
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: btConnecta.top
font.pixelSize: 35
horizontalAlignment: Text.AlignHCenter
anchors.bottomMargin: 25
anchors.rightMargin: 10
anchors.leftMargin: 10
font.bold: true
}
Button {
id: btConnecta
x: 164
y: 214
text: qsTr("&Connecta")
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
}
}
ex07a_02
EstatMqttForm.ui.qml
property alias btLedBlanc: btLedBlanc
Button {
id: btLedBlanc
x: 169
text: qsTr("Encén led blanc")
anchors.top: btConnecta.bottom
anchors.horizontalCenterOffset: 0
anchors.topMargin: 25
anchors.horizontalCenter: btConnecta.horizontalCenter
enabled: false
}
main.qml
title: qsTr("ex07a - v0.2")
EstatMqtt{
anchors.centerIn: parent
btConnecta.onClicked: {
textInfo.text = (textInfo.text === qsTr("MQTT desconnectat"))?qsTr("MQTT connectat"):qsTr("MQTT desconnectat")
textInfo.color = (textInfo.text === qsTr("MQTT desconnectat"))?"#ee2222":"#22ee22"
btConnecta.text = (textInfo.text === qsTr("MQTT desconnectat"))?qsTr("&Connecta"):qsTr("Des&connecta")
btLedBlanc.enabled = (textInfo.text === qsTr("MQTT desconnectat"))?false:true
}
btLedBlanc.onClicked: {
btLedBlanc.text = (btLedBlanc.text === qsTr("Encén led blanc"))?qsTr("Apaga led blanc"):qsTr("Encén led blanc")
}
}
ex07a_03
rerefonsmqtt.h
#ifndef REREFONSMQTT_H
#define REREFONSMQTT_H
#include <QObject>
#include <QMqttClient>
class RerefonsMqtt : public QObject
{
Q_OBJECT
public:
explicit RerefonsMqtt(QObject *parent = nullptr);
signals:
void senyalEstatConnexioMqtt(int nEstatConnexioMqtt);
public slots:
void vBotoConnectaDesconnectaMqtt();
private:
int m_nEstat;
};
#endif // REREFONSMQTT_H
rerefonsmqtt.cpp
#include "rerefonsmqtt.h"
RerefonsMqtt::RerefonsMqtt(QObject *parent) : QObject(parent)
{
m_nEstat = 1;
}
void RerefonsMqtt::vBotoConnectaDesconnectaMqtt(){
if(m_nEstat > 2)
m_nEstat = 0;
emit senyalEstatConnexioMqtt(m_nEstat++);
}
main.qml
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12 // <---
import desDel.rerefonsMqtt 1.0 // <---
ApplicationWindow { // <--- !!!!
width: 640
height: 480
visible: true
title: qsTr("ex07a - v0.3")
RerefonsMqtt{ // <--- !!!!
id: rfMqtt
onSenyalEstatConnexioMqtt: {
console.log(nEstatConnexioMqtt)
}
}
EstatMqtt{
id: eMqtt // <--- !!!!
anchors.centerIn: parent
btConnecta.onClicked: {
textInfo.text = (textInfo.text === qsTr("MQTT desconnectat"))?qsTr("MQTT connectat"):qsTr("MQTT desconnectat")
textInfo.color = (textInfo.text === qsTr("MQTT desconnectat"))?"#ee2222":"#22ee22"
btConnecta.text = (textInfo.text === qsTr("MQTT desconnectat"))?qsTr("&Connecta"):qsTr("Des&connecta")
btLedBlanc.enabled = (textInfo.text === qsTr("MQTT desconnectat"))?false:true
rfMqtt.vBotoConnectaDesconnectaMqtt() // <--- !!!!
}
btLedBlanc.onClicked: {
btLedBlanc.text = (btLedBlanc.text === qsTr("Encén led blanc"))?qsTr("Apaga led blanc"):qsTr("Encén led blanc")
}
}
}