summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Shienkov <denis.shienkov@gmail.com>2017-09-27 16:47:38 (GMT)
committerDenis Shienkov <denis.shienkov@gmail.com>2017-10-06 20:17:15 (GMT)
commitc513cbaee4d8819a2ebb27396be6c056b1f5856f (patch)
tree0a3fe4811ebe62b811a3f4f95ebd8d9602f5af13
parent466705128376bd5d60ec23eec7af37d3bf6649fe (diff)
downloadqtserialport-c513cbaee4d8819a2ebb27396be6c056b1f5856f.tar.gz
Revamp the Blocking Slave Example
* Use the 'const' keywords more. * Split very long lines. * Use the 'm_' prefixes for all class members. * Use the QOverload in signal/slot casting. * Use more member class initializations. * Remove QT_USE_NAMESPACE macro. * Use from(to)Utf8 instead of from(to)Local8Bit. Task-number: QTBUG-60652 Change-Id: I64bfaf93672dc69f4d10bcaaa0c1c6dd87267d3e Reviewed-by: André Hartmann <aha_1980@gmx.de> Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r--examples/serialport/blockingslave/dialog.cpp107
-rw-r--r--examples/serialport/blockingslave/dialog.h26
-rw-r--r--examples/serialport/blockingslave/main.cpp4
-rw-r--r--examples/serialport/blockingslave/slavethread.cpp62
-rw-r--r--examples/serialport/blockingslave/slavethread.h15
5 files changed, 106 insertions, 108 deletions
diff --git a/examples/serialport/blockingslave/dialog.cpp b/examples/serialport/blockingslave/dialog.cpp
index 3bfd419..1723086 100644
--- a/examples/serialport/blockingslave/dialog.cpp
+++ b/examples/serialport/blockingslave/dialog.cpp
@@ -50,95 +50,94 @@
#include "dialog.h"
+#include <QComboBox>
+#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
-#include <QComboBox>
-#include <QSpinBox>
#include <QPushButton>
-#include <QGridLayout>
-
-#include <QtSerialPort/QSerialPortInfo>
-
-QT_USE_NAMESPACE
+#include <QSerialPortInfo>
+#include <QSpinBox>
-Dialog::Dialog(QWidget *parent)
- : QDialog(parent)
- , transactionCount(0)
- , serialPortLabel(new QLabel(tr("Serial port:")))
- , serialPortComboBox(new QComboBox())
- , waitRequestLabel(new QLabel(tr("Wait request, msec:")))
- , waitRequestSpinBox(new QSpinBox())
- , responseLabel(new QLabel(tr("Response:")))
- , responseLineEdit(new QLineEdit(tr("Hello, I'm Slave.")))
- , trafficLabel(new QLabel(tr("No traffic.")))
- , statusLabel(new QLabel(tr("Status: Not running.")))
- , runButton(new QPushButton(tr("Start")))
+Dialog::Dialog(QWidget *parent) :
+ QDialog(parent),
+ m_serialPortLabel(new QLabel(tr("Serial port:"))),
+ m_serialPortComboBox(new QComboBox),
+ m_waitRequestLabel(new QLabel(tr("Wait request, msec:"))),
+ m_waitRequestSpinBox(new QSpinBox),
+ m_responseLabel(new QLabel(tr("Response:"))),
+ m_responseLineEdit(new QLineEdit(tr("Hello, I'm Slave."))),
+ m_trafficLabel(new QLabel(tr("No traffic."))),
+ m_statusLabel(new QLabel(tr("Status: Not running."))),
+ m_runButton(new QPushButton(tr("Start")))
{
- waitRequestSpinBox->setRange(0, 10000);
- waitRequestSpinBox->setValue(10000);
+ m_waitRequestSpinBox->setRange(0, 10000);
+ m_waitRequestSpinBox->setValue(10000);
const auto infos = QSerialPortInfo::availablePorts();
for (const QSerialPortInfo &info : infos)
- serialPortComboBox->addItem(info.portName());
+ m_serialPortComboBox->addItem(info.portName());
auto mainLayout = new QGridLayout;
- mainLayout->addWidget(serialPortLabel, 0, 0);
- mainLayout->addWidget(serialPortComboBox, 0, 1);
- mainLayout->addWidget(waitRequestLabel, 1, 0);
- mainLayout->addWidget(waitRequestSpinBox, 1, 1);
- mainLayout->addWidget(runButton, 0, 2, 2, 1);
- mainLayout->addWidget(responseLabel, 2, 0);
- mainLayout->addWidget(responseLineEdit, 2, 1, 1, 3);
- mainLayout->addWidget(trafficLabel, 3, 0, 1, 4);
- mainLayout->addWidget(statusLabel, 4, 0, 1, 5);
+ mainLayout->addWidget(m_serialPortLabel, 0, 0);
+ mainLayout->addWidget(m_serialPortComboBox, 0, 1);
+ mainLayout->addWidget(m_waitRequestLabel, 1, 0);
+ mainLayout->addWidget(m_waitRequestSpinBox, 1, 1);
+ mainLayout->addWidget(m_runButton, 0, 2, 2, 1);
+ mainLayout->addWidget(m_responseLabel, 2, 0);
+ mainLayout->addWidget(m_responseLineEdit, 2, 1, 1, 3);
+ mainLayout->addWidget(m_trafficLabel, 3, 0, 1, 4);
+ mainLayout->addWidget(m_statusLabel, 4, 0, 1, 5);
setLayout(mainLayout);
setWindowTitle(tr("Blocking Slave"));
- serialPortComboBox->setFocus();
+ m_serialPortComboBox->setFocus();
- connect(runButton, &QPushButton::clicked, this, &Dialog::startSlave);
- connect(&thread, &SlaveThread::request, this,&Dialog::showRequest);
- connect(&thread, &SlaveThread::error, this, &Dialog::processError);
- connect(&thread, &SlaveThread::timeout, this, &Dialog::processTimeout);
+ connect(m_runButton, &QPushButton::clicked, this, &Dialog::startSlave);
+ connect(&m_thread, &SlaveThread::request, this,&Dialog::showRequest);
+ connect(&m_thread, &SlaveThread::error, this, &Dialog::processError);
+ connect(&m_thread, &SlaveThread::timeout, this, &Dialog::processTimeout);
- connect(serialPortComboBox, static_cast<void (QComboBox::*)(const QString &)>(&QComboBox::currentIndexChanged),
+ connect(m_serialPortComboBox, QOverload<const QString &>::of(&QComboBox::currentIndexChanged),
this, &Dialog::activateRunButton);
- connect(waitRequestSpinBox, static_cast<void (QSpinBox::*)(const QString &)>(&QSpinBox::valueChanged),
+ connect(m_waitRequestSpinBox, QOverload<const QString &>::of(&QSpinBox::valueChanged),
this, &Dialog::activateRunButton);
- connect(responseLineEdit, &QLineEdit::textChanged, this, &Dialog::activateRunButton);
+ connect(m_responseLineEdit, &QLineEdit::textChanged, this, &Dialog::activateRunButton);
}
void Dialog::startSlave()
{
- runButton->setEnabled(false);
- statusLabel->setText(tr("Status: Running, connected to port %1.")
- .arg(serialPortComboBox->currentText()));
- thread.startSlave(serialPortComboBox->currentText(),
- waitRequestSpinBox->value(),
- responseLineEdit->text());
+ m_runButton->setEnabled(false);
+ m_statusLabel->setText(tr("Status: Running, connected to port %1.")
+ .arg(m_serialPortComboBox->currentText()));
+ m_thread.startSlave(m_serialPortComboBox->currentText(),
+ m_waitRequestSpinBox->value(),
+ m_responseLineEdit->text());
}
void Dialog::showRequest(const QString &s)
{
- trafficLabel->setText(tr("Traffic, transaction #%1:"
- "\n\r-request: %2"
- "\n\r-response: %3")
- .arg(++transactionCount).arg(s).arg(responseLineEdit->text()));
+ m_trafficLabel->setText(tr("Traffic, transaction #%1:"
+ "\n\r-request: %2"
+ "\n\r-response: %3")
+ .arg(++m_transactionCount)
+ .arg(s)
+ .arg(m_responseLineEdit->text()));
}
void Dialog::processError(const QString &s)
{
activateRunButton();
- statusLabel->setText(tr("Status: Not running, %1.").arg(s));
- trafficLabel->setText(tr("No traffic."));
+ m_statusLabel->setText(tr("Status: Not running, %1.").arg(s));
+ m_trafficLabel->setText(tr("No traffic."));
}
void Dialog::processTimeout(const QString &s)
{
- statusLabel->setText(tr("Status: Running, %1.").arg(s));
- trafficLabel->setText(tr("No traffic."));
+ m_statusLabel->setText(tr("Status: Running, %1.").arg(s));
+ m_trafficLabel->setText(tr("No traffic."));
}
+
void Dialog::activateRunButton()
{
- runButton->setEnabled(true);
+ m_runButton->setEnabled(true);
}
diff --git a/examples/serialport/blockingslave/dialog.h b/examples/serialport/blockingslave/dialog.h
index 0e59410..c8f80a6 100644
--- a/examples/serialport/blockingslave/dialog.h
+++ b/examples/serialport/blockingslave/dialog.h
@@ -51,10 +51,10 @@
#ifndef DIALOG_H
#define DIALOG_H
-#include <QDialog>
-
#include "slavethread.h"
+#include <QDialog>
+
QT_BEGIN_NAMESPACE
class QLabel;
@@ -80,18 +80,18 @@ private slots:
void activateRunButton();
private:
- int transactionCount;
- QLabel *serialPortLabel;
- QComboBox *serialPortComboBox;
- QLabel *waitRequestLabel;
- QSpinBox *waitRequestSpinBox;
- QLabel *responseLabel;
- QLineEdit *responseLineEdit;
- QLabel *trafficLabel;
- QLabel *statusLabel;
- QPushButton *runButton;
+ int m_transactionCount = 0;
+ QLabel *m_serialPortLabel = nullptr;
+ QComboBox *m_serialPortComboBox = nullptr;
+ QLabel *m_waitRequestLabel = nullptr;
+ QSpinBox *m_waitRequestSpinBox = nullptr;
+ QLabel *m_responseLabel = nullptr;
+ QLineEdit *m_responseLineEdit = nullptr;
+ QLabel *m_trafficLabel = nullptr;
+ QLabel *m_statusLabel = nullptr;
+ QPushButton *m_runButton = nullptr;
- SlaveThread thread;
+ SlaveThread m_thread;
};
#endif // DIALOG_H
diff --git a/examples/serialport/blockingslave/main.cpp b/examples/serialport/blockingslave/main.cpp
index cf4c421..8e68c73 100644
--- a/examples/serialport/blockingslave/main.cpp
+++ b/examples/serialport/blockingslave/main.cpp
@@ -48,10 +48,10 @@
**
****************************************************************************/
-#include <QApplication>
-
#include "dialog.h"
+#include <QApplication>
+
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
diff --git a/examples/serialport/blockingslave/slavethread.cpp b/examples/serialport/blockingslave/slavethread.cpp
index 66bfb19..85b010a 100644
--- a/examples/serialport/blockingslave/slavethread.cpp
+++ b/examples/serialport/blockingslave/slavethread.cpp
@@ -50,22 +50,20 @@
#include "slavethread.h"
-#include <QtSerialPort/QSerialPort>
-
+#include <QSerialPort>
#include <QTime>
-QT_USE_NAMESPACE
-
-SlaveThread::SlaveThread(QObject *parent)
- : QThread(parent), waitTimeout(0), quit(false)
+SlaveThread::SlaveThread(QObject *parent) :
+ QThread(parent)
{
}
+
//! [0]
SlaveThread::~SlaveThread()
{
- mutex.lock();
- quit = true;
- mutex.unlock();
+ m_mutex.lock();
+ m_quit = true;
+ m_mutex.unlock();
wait();
}
//! [0]
@@ -73,12 +71,12 @@ SlaveThread::~SlaveThread()
//! [1] //! [2]
void SlaveThread::startSlave(const QString &portName, int waitTimeout, const QString &response)
{
-//! [1]
- QMutexLocker locker(&mutex);
- this->portName = portName;
- this->waitTimeout = waitTimeout;
- this->response = response;
-//! [3]
+ //! [1]
+ const QMutexLocker locker(&m_mutex);
+ m_portName = portName;
+ m_waitTimeout = waitTimeout;
+ m_response = response;
+ //! [3]
if (!isRunning())
start();
}
@@ -89,21 +87,21 @@ void SlaveThread::run()
{
bool currentPortNameChanged = false;
- mutex.lock();
+ m_mutex.lock();
//! [4] //! [5]
QString currentPortName;
- if (currentPortName != portName) {
- currentPortName = portName;
+ if (currentPortName != m_portName) {
+ currentPortName = m_portName;
currentPortNameChanged = true;
}
- int currentWaitTimeout = waitTimeout;
- QString currentRespone = response;
- mutex.unlock();
+ int currentWaitTimeout = m_waitTimeout;
+ QString currentRespone = m_response;
+ m_mutex.unlock();
//! [5] //! [6]
QSerialPort serial;
- while (!quit) {
+ while (!m_quit) {
//![6] //! [7]
if (currentPortNameChanged) {
serial.close();
@@ -111,7 +109,7 @@ void SlaveThread::run()
if (!serial.open(QIODevice::ReadWrite)) {
emit error(tr("Can't open %1, error code %2")
- .arg(portName).arg(serial.error()));
+ .arg(m_portName).arg(serial.error()));
return;
}
}
@@ -124,10 +122,10 @@ void SlaveThread::run()
requestData += serial.readAll();
//! [8] //! [10]
// write response
- QByteArray responseData = currentRespone.toLocal8Bit();
+ const QByteArray responseData = currentRespone.toUtf8();
serial.write(responseData);
- if (serial.waitForBytesWritten(waitTimeout)) {
- QString request(requestData);
+ if (serial.waitForBytesWritten(m_waitTimeout)) {
+ const QString request = QString::fromUtf8(requestData);
//! [12]
emit this->request(request);
//! [10] //! [11] //! [12]
@@ -141,16 +139,16 @@ void SlaveThread::run()
.arg(QTime::currentTime().toString()));
}
//! [9] //! [13]
- mutex.lock();
- if (currentPortName != portName) {
- currentPortName = portName;
+ m_mutex.lock();
+ if (currentPortName != m_portName) {
+ currentPortName = m_portName;
currentPortNameChanged = true;
} else {
currentPortNameChanged = false;
}
- currentWaitTimeout = waitTimeout;
- currentRespone = response;
- mutex.unlock();
+ currentWaitTimeout = m_waitTimeout;
+ currentRespone = m_response;
+ m_mutex.unlock();
}
//! [13]
}
diff --git a/examples/serialport/blockingslave/slavethread.h b/examples/serialport/blockingslave/slavethread.h
index 9a15e24..737e7cb 100644
--- a/examples/serialport/blockingslave/slavethread.h
+++ b/examples/serialport/blockingslave/slavethread.h
@@ -51,8 +51,8 @@
#ifndef SLAVETHREAD_H
#define SLAVETHREAD_H
-#include <QThread>
#include <QMutex>
+#include <QThread>
#include <QWaitCondition>
//! [0]
@@ -65,7 +65,6 @@ public:
~SlaveThread();
void startSlave(const QString &portName, int waitTimeout, const QString &response);
- void run() Q_DECL_OVERRIDE;
signals:
void request(const QString &s);
@@ -73,11 +72,13 @@ signals:
void timeout(const QString &s);
private:
- QString portName;
- QString response;
- int waitTimeout;
- QMutex mutex;
- bool quit;
+ void run() override;
+
+ QString m_portName;
+ QString m_response;
+ int m_waitTimeout = 0;
+ QMutex m_mutex;
+ bool m_quit = false;
};
//! [0]