mirror of
https://github.com/qt/qtmqtt.git
synced 2026-10-25 16:16:26 +00:00
Make example a bit more verbose
This commit is contained in:
parent
d67f06e583
commit
b0d17c2196
@ -1,13 +1,9 @@
|
||||
#include "mainwindow.h"
|
||||
#include "ui_mainwindow.h"
|
||||
|
||||
#include <QtCore/QDateTime>
|
||||
#include <QtMqtt/QMqttClient>
|
||||
|
||||
// http://test.mosquitto.org/
|
||||
//const QString TEST_HOST("test.mosquitto.org");
|
||||
const QString TEST_HOST("hostprocess.de");
|
||||
const quint16 TEST_PORT = 1883;
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent) :
|
||||
QMainWindow(parent),
|
||||
ui(new Ui::MainWindow)
|
||||
@ -15,8 +11,16 @@ MainWindow::MainWindow(QWidget *parent) :
|
||||
ui->setupUi(this);
|
||||
|
||||
m_client = new QMqttClient(this);
|
||||
m_client->setHostname(TEST_HOST);
|
||||
m_client->setPort(TEST_PORT);
|
||||
m_client->setHostname(ui->lineEditHost->text());
|
||||
m_client->setPort(ui->spinBoxPort->value());
|
||||
|
||||
connect(m_client, &QMqttClient::stateChanged, this, &MainWindow::updateLogStateChange);
|
||||
connect(m_client, &QMqttClient::disconnected, this, &MainWindow::brokerDisconnected);
|
||||
connect(ui->lineEditHost, &QLineEdit::textChanged, m_client, &QMqttClient::setHostname);
|
||||
//connect(ui->spinBoxPort, &QSpinBox::valueChanged, m_client, &QMqttClient::setPort);
|
||||
|
||||
connect(ui->spinBoxPort, SIGNAL(valueChanged(int)), m_client, SLOT(setPort(quint16)));
|
||||
updateLogStateChange();
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
@ -26,10 +30,36 @@ MainWindow::~MainWindow()
|
||||
|
||||
void MainWindow::on_buttonConnect_clicked()
|
||||
{
|
||||
m_client->connectToHost();
|
||||
if (m_client->state() == QMqttClient::Disconnected) {
|
||||
ui->lineEditHost->setEnabled(false);
|
||||
ui->spinBoxPort->setEnabled(false);
|
||||
ui->buttonConnect->setText(tr("Disconnect"));
|
||||
m_client->connectToHost();
|
||||
} else {
|
||||
ui->lineEditHost->setEnabled(true);
|
||||
ui->spinBoxPort->setEnabled(true);
|
||||
ui->buttonConnect->setText(tr("Connect"));
|
||||
m_client->disconnectFromHost();
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::on_buttonQuit_clicked()
|
||||
{
|
||||
QApplication::quit();
|
||||
}
|
||||
|
||||
void MainWindow::updateLogStateChange()
|
||||
{
|
||||
const QString content = QDateTime::currentDateTime().toString()
|
||||
+ QLatin1String(": State Change")
|
||||
+ QString::number(m_client->state())
|
||||
+ QLatin1Char('\n');
|
||||
ui->editLog->insertPlainText(content);
|
||||
}
|
||||
|
||||
void MainWindow::brokerDisconnected()
|
||||
{
|
||||
ui->lineEditHost->setEnabled(true);
|
||||
ui->spinBoxPort->setEnabled(true);
|
||||
ui->buttonConnect->setText(tr("Connect"));
|
||||
}
|
||||
|
||||
@ -20,6 +20,9 @@ public:
|
||||
private slots:
|
||||
void on_buttonConnect_clicked();
|
||||
void on_buttonQuit_clicked();
|
||||
void updateLogStateChange();
|
||||
|
||||
void brokerDisconnected();
|
||||
|
||||
private:
|
||||
Ui::MainWindow *ui;
|
||||
|
||||
@ -6,8 +6,8 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>400</width>
|
||||
<height>300</height>
|
||||
<width>492</width>
|
||||
<height>445</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
@ -16,18 +16,86 @@
|
||||
<widget class="QWidget" name="centralWidget">
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QPushButton" name="buttonConnect">
|
||||
<property name="text">
|
||||
<string>Connect</string>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<layout class="QFormLayout" name="formLayout">
|
||||
<item row="0" column="0">
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>Host:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QLineEdit" name="lineEditHost">
|
||||
<property name="text">
|
||||
<string>hostprocess.de</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="0">
|
||||
<widget class="QLabel" name="label_2">
|
||||
<property name="text">
|
||||
<string>Port:</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="2" column="1">
|
||||
<widget class="QSpinBox" name="spinBoxPort">
|
||||
<property name="maximum">
|
||||
<number>99999</number>
|
||||
</property>
|
||||
<property name="value">
|
||||
<number>1883</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="buttonConnect">
|
||||
<property name="text">
|
||||
<string>Connect</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QGroupBox" name="groupBox">
|
||||
<property name="title">
|
||||
<string>Log Messages</string>
|
||||
</property>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||
<item>
|
||||
<widget class="QPlainTextEdit" name="editLog"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="buttonQuit">
|
||||
<property name="text">
|
||||
<string>Quit</string>
|
||||
</property>
|
||||
</widget>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_3">
|
||||
<item>
|
||||
<spacer name="horizontalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="buttonQuit">
|
||||
<property name="text">
|
||||
<string>Quit</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
@ -36,10 +104,17 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>400</width>
|
||||
<width>492</width>
|
||||
<height>21</height>
|
||||
</rect>
|
||||
</property>
|
||||
<widget class="QMenu" name="menuFile">
|
||||
<property name="title">
|
||||
<string>File</string>
|
||||
</property>
|
||||
<addaction name="actionQuit"/>
|
||||
</widget>
|
||||
<addaction name="menuFile"/>
|
||||
</widget>
|
||||
<widget class="QToolBar" name="mainToolBar">
|
||||
<attribute name="toolBarArea">
|
||||
@ -50,6 +125,11 @@
|
||||
</attribute>
|
||||
</widget>
|
||||
<widget class="QStatusBar" name="statusBar"/>
|
||||
<action name="actionQuit">
|
||||
<property name="text">
|
||||
<string>Quit</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<layoutdefault spacing="6" margin="11"/>
|
||||
<resources/>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user