summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Shienkov <denis.shienkov@gmail.com>2017-09-27 16:56:10 (GMT)
committerDenis Shienkov <denis.shienkov@gmail.com>2017-10-06 20:17:47 (GMT)
commit07e1fc7bb93dc4c306d8e64f4cb88d42177cba2e (patch)
tree7ffe338ff5428cd5e596ac3ed069820b4a01bc41
parentc513cbaee4d8819a2ebb27396be6c056b1f5856f (diff)
downloadqtserialport-07e1fc7bb93dc4c306d8e64f4cb88d42177cba2e.tar.gz
Revamp the Blocking Master 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: I5ef9916cc1e6371dde97154f5f5683f85bc02e96 Reviewed-by: André Hartmann <aha_1980@gmx.de>
-rw-r--r--examples/serialport/blockingmaster/dialog.cpp104
-rw-r--r--examples/serialport/blockingmaster/dialog.h26
-rw-r--r--examples/serialport/blockingmaster/main.cpp4
-rw-r--r--examples/serialport/blockingmaster/masterthread.cpp87
-rw-r--r--examples/serialport/blockingmaster/masterthread.h17
5 files changed, 117 insertions, 121 deletions
diff --git a/examples/serialport/blockingmaster/dialog.cpp b/examples/serialport/blockingmaster/dialog.cpp
index 08403c3..2617da7 100644
--- a/examples/serialport/blockingmaster/dialog.cpp
+++ b/examples/serialport/blockingmaster/dialog.cpp
@@ -50,95 +50,93 @@
#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())
- , waitResponseLabel(new QLabel(tr("Wait response, msec:")))
- , waitResponseSpinBox(new QSpinBox())
- , requestLabel(new QLabel(tr("Request:")))
- , requestLineEdit(new QLineEdit(tr("Who are you?")))
- , 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_waitResponseLabel(new QLabel(tr("Wait response, msec:"))),
+ m_waitResponseSpinBox(new QSpinBox),
+ m_requestLabel(new QLabel(tr("Request:"))),
+ m_requestLineEdit(new QLineEdit(tr("Who are you?"))),
+ m_trafficLabel(new QLabel(tr("No traffic."))),
+ m_statusLabel(new QLabel(tr("Status: Not running."))),
+ m_runButton(new QPushButton(tr("Start")))
{
const auto infos = QSerialPortInfo::availablePorts();
for (const QSerialPortInfo &info : infos)
- serialPortComboBox->addItem(info.portName());
+ m_serialPortComboBox->addItem(info.portName());
- waitResponseSpinBox->setRange(0, 10000);
- waitResponseSpinBox->setValue(1000);
+ m_waitResponseSpinBox->setRange(0, 10000);
+ m_waitResponseSpinBox->setValue(1000);
auto mainLayout = new QGridLayout;
- mainLayout->addWidget(serialPortLabel, 0, 0);
- mainLayout->addWidget(serialPortComboBox, 0, 1);
- mainLayout->addWidget(waitResponseLabel, 1, 0);
- mainLayout->addWidget(waitResponseSpinBox, 1, 1);
- mainLayout->addWidget(runButton, 0, 2, 2, 1);
- mainLayout->addWidget(requestLabel, 2, 0);
- mainLayout->addWidget(requestLineEdit, 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_waitResponseLabel, 1, 0);
+ mainLayout->addWidget(m_waitResponseSpinBox, 1, 1);
+ mainLayout->addWidget(m_runButton, 0, 2, 2, 1);
+ mainLayout->addWidget(m_requestLabel, 2, 0);
+ mainLayout->addWidget(m_requestLineEdit, 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 Master"));
- serialPortComboBox->setFocus();
+ m_serialPortComboBox->setFocus();
- connect(runButton, &QPushButton::clicked, this, &Dialog::transaction);
- connect(&thread, &MasterThread::response, this, &Dialog::showResponse);
- connect(&thread, &MasterThread::error, this, &Dialog::processError);
- connect(&thread, &MasterThread::timeout, this, &Dialog::processTimeout);
+ connect(m_runButton, &QPushButton::clicked, this, &Dialog::transaction);
+ connect(&m_thread, &MasterThread::response, this, &Dialog::showResponse);
+ connect(&m_thread, &MasterThread::error, this, &Dialog::processError);
+ connect(&m_thread, &MasterThread::timeout, this, &Dialog::processTimeout);
}
void Dialog::transaction()
{
setControlsEnabled(false);
- statusLabel->setText(tr("Status: Running, connected to port %1.")
- .arg(serialPortComboBox->currentText()));
- thread.transaction(serialPortComboBox->currentText(),
- waitResponseSpinBox->value(),
- requestLineEdit->text());
+ m_statusLabel->setText(tr("Status: Running, connected to port %1.")
+ .arg(m_serialPortComboBox->currentText()));
+ m_thread.transaction(m_serialPortComboBox->currentText(),
+ m_waitResponseSpinBox->value(),
+ m_requestLineEdit->text());
}
void Dialog::showResponse(const QString &s)
{
setControlsEnabled(true);
- trafficLabel->setText(tr("Traffic, transaction #%1:"
- "\n\r-request: %2"
- "\n\r-response: %3")
- .arg(++transactionCount).arg(requestLineEdit->text()).arg(s));
+ m_trafficLabel->setText(tr("Traffic, transaction #%1:"
+ "\n\r-request: %2"
+ "\n\r-response: %3")
+ .arg(++m_transactionCount)
+ .arg(m_requestLineEdit->text())
+ .arg(s));
}
void Dialog::processError(const QString &s)
{
setControlsEnabled(true);
- 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)
{
setControlsEnabled(true);
- 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::setControlsEnabled(bool enable)
{
- runButton->setEnabled(enable);
- serialPortComboBox->setEnabled(enable);
- waitResponseSpinBox->setEnabled(enable);
- requestLineEdit->setEnabled(enable);
+ m_runButton->setEnabled(enable);
+ m_serialPortComboBox->setEnabled(enable);
+ m_waitResponseSpinBox->setEnabled(enable);
+ m_requestLineEdit->setEnabled(enable);
}
diff --git a/examples/serialport/blockingmaster/dialog.h b/examples/serialport/blockingmaster/dialog.h
index aff8177..8d997fa 100644
--- a/examples/serialport/blockingmaster/dialog.h
+++ b/examples/serialport/blockingmaster/dialog.h
@@ -51,10 +51,10 @@
#ifndef DIALOG_H
#define DIALOG_H
-#include <QDialog>
-
#include "masterthread.h"
+#include <QDialog>
+
QT_BEGIN_NAMESPACE
class QLabel;
@@ -82,18 +82,18 @@ private:
void setControlsEnabled(bool enable);
private:
- int transactionCount;
- QLabel *serialPortLabel;
- QComboBox *serialPortComboBox;
- QLabel *waitResponseLabel;
- QSpinBox *waitResponseSpinBox;
- QLabel *requestLabel;
- QLineEdit *requestLineEdit;
- QLabel *trafficLabel;
- QLabel *statusLabel;
- QPushButton *runButton;
+ int m_transactionCount = 0;
+ QLabel *m_serialPortLabel = nullptr;
+ QComboBox *m_serialPortComboBox = nullptr;
+ QLabel *m_waitResponseLabel = nullptr;
+ QSpinBox *m_waitResponseSpinBox = nullptr;
+ QLabel *m_requestLabel = nullptr;
+ QLineEdit *m_requestLineEdit = nullptr;
+ QLabel *m_trafficLabel = nullptr;
+ QLabel *m_statusLabel = nullptr;
+ QPushButton *m_runButton = nullptr;
- MasterThread thread;
+ MasterThread m_thread;
};
#endif // DIALOG_H
diff --git a/examples/serialport/blockingmaster/main.cpp b/examples/serialport/blockingmaster/main.cpp
index cf4c421..8e68c73 100644
--- a/examples/serialport/blockingmaster/main.cpp
+++ b/examples/serialport/blockingmaster/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/blockingmaster/masterthread.cpp b/examples/serialport/blockingmaster/masterthread.cpp
index c81c819..12d23bb 100644
--- a/examples/serialport/blockingmaster/masterthread.cpp
+++ b/examples/serialport/blockingmaster/masterthread.cpp
@@ -50,24 +50,21 @@
#include "masterthread.h"
-#include <QtSerialPort/QSerialPort>
-
+#include <QSerialPort>
#include <QTime>
-QT_USE_NAMESPACE
-
-MasterThread::MasterThread(QObject *parent)
- : QThread(parent), waitTimeout(0), quit(false)
+MasterThread::MasterThread(QObject *parent) :
+ QThread(parent)
{
}
//! [0]
MasterThread::~MasterThread()
{
- mutex.lock();
- quit = true;
- cond.wakeOne();
- mutex.unlock();
+ m_mutex.lock();
+ m_quit = true;
+ m_cond.wakeOne();
+ m_mutex.unlock();
wait();
}
//! [0]
@@ -75,16 +72,16 @@ MasterThread::~MasterThread()
//! [1] //! [2]
void MasterThread::transaction(const QString &portName, int waitTimeout, const QString &request)
{
- //! [1]
- QMutexLocker locker(&mutex);
- this->portName = portName;
- this->waitTimeout = waitTimeout;
- this->request = request;
- //! [3]
+//! [1]
+ const QMutexLocker locker(&m_mutex);
+ m_portName = portName;
+ m_waitTimeout = waitTimeout;
+ m_request = request;
+//! [3]
if (!isRunning())
start();
else
- cond.wakeOne();
+ m_cond.wakeOne();
}
//! [2] //! [3]
@@ -93,69 +90,69 @@ void MasterThread::run()
{
bool currentPortNameChanged = false;
- mutex.lock();
- //! [4] //! [5]
+ 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 currentRequest = request;
- mutex.unlock();
- //! [5] //! [6]
+ int currentWaitTimeout = m_waitTimeout;
+ QString currentRequest = m_request;
+ m_mutex.unlock();
+//! [5] //! [6]
QSerialPort serial;
- while (!quit) {
- //![6] //! [7]
+ while (!m_quit) {
+//![6] //! [7]
if (currentPortNameChanged) {
serial.close();
serial.setPortName(currentPortName);
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;
}
}
- //! [7] //! [8]
+//! [7] //! [8]
// write request
- QByteArray requestData = currentRequest.toLocal8Bit();
+ const QByteArray requestData = currentRequest.toUtf8();
serial.write(requestData);
- if (serial.waitForBytesWritten(waitTimeout)) {
- //! [8] //! [10]
+ if (serial.waitForBytesWritten(m_waitTimeout)) {
+//! [8] //! [10]
// read response
if (serial.waitForReadyRead(currentWaitTimeout)) {
QByteArray responseData = serial.readAll();
while (serial.waitForReadyRead(10))
responseData += serial.readAll();
- QString response(responseData);
- //! [12]
+ const QString response = QString::fromUtf8(responseData);
+//! [12]
emit this->response(response);
- //! [10] //! [11] //! [12]
+//! [10] //! [11] //! [12]
} else {
emit timeout(tr("Wait read response timeout %1")
.arg(QTime::currentTime().toString()));
}
- //! [9] //! [11]
+//! [9] //! [11]
} else {
emit timeout(tr("Wait write request timeout %1")
.arg(QTime::currentTime().toString()));
}
- //! [9] //! [13]
- mutex.lock();
- cond.wait(&mutex);
- if (currentPortName != portName) {
- currentPortName = portName;
+//! [9] //! [13]
+ m_mutex.lock();
+ m_cond.wait(&m_mutex);
+ if (currentPortName != m_portName) {
+ currentPortName = m_portName;
currentPortNameChanged = true;
} else {
currentPortNameChanged = false;
}
- currentWaitTimeout = waitTimeout;
- currentRequest = request;
- mutex.unlock();
+ currentWaitTimeout = m_waitTimeout;
+ currentRequest = m_request;
+ m_mutex.unlock();
}
- //! [13]
+//! [13]
}
diff --git a/examples/serialport/blockingmaster/masterthread.h b/examples/serialport/blockingmaster/masterthread.h
index f23559f..9dc6048 100644
--- a/examples/serialport/blockingmaster/masterthread.h
+++ b/examples/serialport/blockingmaster/masterthread.h
@@ -51,8 +51,8 @@
#ifndef MASTERTHREAD_H
#define MASTERTHREAD_H
-#include <QThread>
#include <QMutex>
+#include <QThread>
#include <QWaitCondition>
//! [0]
@@ -65,7 +65,6 @@ public:
~MasterThread();
void transaction(const QString &portName, int waitTimeout, const QString &request);
- void run() Q_DECL_OVERRIDE;
signals:
void response(const QString &s);
@@ -73,12 +72,14 @@ signals:
void timeout(const QString &s);
private:
- QString portName;
- QString request;
- int waitTimeout;
- QMutex mutex;
- QWaitCondition cond;
- bool quit;
+ void run() override;
+
+ QString m_portName;
+ QString m_request;
+ int m_waitTimeout = 0;
+ QMutex m_mutex;
+ QWaitCondition m_cond;
+ bool m_quit = false;
};
//! [0]