summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Shienkov <denis.shienkov@gmail.com>2017-09-27 16:31:15 (GMT)
committerDenis Shienkov <denis.shienkov@gmail.com>2017-10-05 06:44:51 (GMT)
commit466705128376bd5d60ec23eec7af37d3bf6649fe (patch)
treec8b4b4b7ac72753abde602ecbbae907f9a5c8aab
parentda09f8f01d13ff467f0a628b6348aa9300160875 (diff)
downloadqtserialport-466705128376bd5d60ec23eec7af37d3bf6649fe.tar.gz
Revamp the Console Asynchronous Reader Example
* Use the 'const' keywords more. * Split very long lines. * Remove QT_USE_NAMESPACE macro. * Update includes to common style. * Use more member class initializations. * Remove empy dtor. Task-number: QTBUG-60652 Change-Id: I9cd0e010f2489993141ed4e24557e492201c98b6 Reviewed-by: André Hartmann <aha_1980@gmx.de> Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r--examples/serialport/creaderasync/main.cpp26
-rw-r--r--examples/serialport/creaderasync/serialportreader.cpp32
-rw-r--r--examples/serialport/creaderasync/serialportreader.h17
3 files changed, 37 insertions, 38 deletions
diff --git a/examples/serialport/creaderasync/main.cpp b/examples/serialport/creaderasync/main.cpp
index dd1f755..6c669ce 100644
--- a/examples/serialport/creaderasync/main.cpp
+++ b/examples/serialport/creaderasync/main.cpp
@@ -50,37 +50,39 @@
#include "serialportreader.h"
-#include <QtSerialPort/QSerialPort>
-
-#include <QTextStream>
#include <QCoreApplication>
-#include <QFile>
+#include <QSerialPort>
#include <QStringList>
-
-QT_USE_NAMESPACE
+#include <QTextStream>
int main(int argc, char *argv[])
{
QCoreApplication coreApplication(argc, argv);
- int argumentCount = QCoreApplication::arguments().size();
- QStringList argumentList = QCoreApplication::arguments();
+ const int argumentCount = QCoreApplication::arguments().size();
+ const QStringList argumentList = QCoreApplication::arguments();
QTextStream standardOutput(stdout);
if (argumentCount == 1) {
- standardOutput << QObject::tr("Usage: %1 <serialportname> [baudrate]").arg(argumentList.first()) << endl;
+ standardOutput << QObject::tr("Usage: %1 <serialportname> [baudrate]")
+ .arg(argumentList.first())
+ << endl;
return 1;
}
QSerialPort serialPort;
- QString serialPortName = argumentList.at(1);
+ const QString serialPortName = argumentList.at(1);
serialPort.setPortName(serialPortName);
- int serialPortBaudRate = (argumentCount > 2) ? argumentList.at(2).toInt() : QSerialPort::Baud9600;
+ const int serialPortBaudRate = (argumentCount > 2)
+ ? argumentList.at(2).toInt() : QSerialPort::Baud9600;
serialPort.setBaudRate(serialPortBaudRate);
if (!serialPort.open(QIODevice::ReadOnly)) {
- standardOutput << QObject::tr("Failed to open port %1, error: %2").arg(serialPortName).arg(serialPort.errorString()) << endl;
+ standardOutput << QObject::tr("Failed to open port %1, error: %2")
+ .arg(serialPortName)
+ .arg(serialPort.errorString())
+ << endl;
return 1;
}
diff --git a/examples/serialport/creaderasync/serialportreader.cpp b/examples/serialport/creaderasync/serialportreader.cpp
index f85dd8a..a701839 100644
--- a/examples/serialport/creaderasync/serialportreader.cpp
+++ b/examples/serialport/creaderasync/serialportreader.cpp
@@ -52,25 +52,18 @@
#include <QCoreApplication>
-QT_USE_NAMESPACE
-
-SerialPortReader::SerialPortReader(QSerialPort *serialPort, QObject *parent)
- : QObject(parent)
- , m_serialPort(serialPort)
- , m_standardOutput(stdout)
+SerialPortReader::SerialPortReader(QSerialPort *serialPort, QObject *parent) :
+ QObject(parent),
+ m_serialPort(serialPort),
+ m_standardOutput(stdout)
{
connect(m_serialPort, &QSerialPort::readyRead, this, &SerialPortReader::handleReadyRead);
- connect(m_serialPort, static_cast<void (QSerialPort::*)(QSerialPort::SerialPortError)>(&QSerialPort::error),
- this, &SerialPortReader::handleError);
+ connect(m_serialPort, &QSerialPort::errorOccurred, this, &SerialPortReader::handleError);
connect(&m_timer, &QTimer::timeout, this, &SerialPortReader::handleTimeout);
m_timer.start(5000);
}
-SerialPortReader::~SerialPortReader()
-{
-}
-
void SerialPortReader::handleReadyRead()
{
m_readData.append(m_serialPort->readAll());
@@ -82,9 +75,14 @@ void SerialPortReader::handleReadyRead()
void SerialPortReader::handleTimeout()
{
if (m_readData.isEmpty()) {
- m_standardOutput << QObject::tr("No data was currently available for reading from port %1").arg(m_serialPort->portName()) << endl;
+ m_standardOutput << QObject::tr("No data was currently available "
+ "for reading from port %1")
+ .arg(m_serialPort->portName())
+ << endl;
} else {
- m_standardOutput << QObject::tr("Data successfully received from port %1").arg(m_serialPort->portName()) << endl;
+ m_standardOutput << QObject::tr("Data successfully received from port %1")
+ .arg(m_serialPort->portName())
+ << endl;
m_standardOutput << m_readData << endl;
}
@@ -94,7 +92,11 @@ void SerialPortReader::handleTimeout()
void SerialPortReader::handleError(QSerialPort::SerialPortError serialPortError)
{
if (serialPortError == QSerialPort::ReadError) {
- m_standardOutput << QObject::tr("An I/O error occurred while reading the data from port %1, error: %2").arg(m_serialPort->portName()).arg(m_serialPort->errorString()) << endl;
+ m_standardOutput << QObject::tr("An I/O error occurred while reading "
+ "the data from port %1, error: %2")
+ .arg(m_serialPort->portName())
+ .arg(m_serialPort->errorString())
+ << endl;
QCoreApplication::exit(1);
}
}
diff --git a/examples/serialport/creaderasync/serialportreader.h b/examples/serialport/creaderasync/serialportreader.h
index 6698087..53074f1 100644
--- a/examples/serialport/creaderasync/serialportreader.h
+++ b/examples/serialport/creaderasync/serialportreader.h
@@ -51,14 +51,10 @@
#ifndef SERIALPORTREADER_H
#define SERIALPORTREADER_H
-#include <QtSerialPort/QSerialPort>
-
+#include <QByteArray>
+#include <QSerialPort>
#include <QTextStream>
#include <QTimer>
-#include <QByteArray>
-#include <QObject>
-
-QT_USE_NAMESPACE
QT_BEGIN_NAMESPACE
@@ -70,7 +66,6 @@ class SerialPortReader : public QObject
public:
explicit SerialPortReader(QSerialPort *serialPort, QObject *parent = nullptr);
- ~SerialPortReader();
private slots:
void handleReadyRead();
@@ -78,10 +73,10 @@ private slots:
void handleError(QSerialPort::SerialPortError error);
private:
- QSerialPort *m_serialPort;
- QByteArray m_readData;
+ QSerialPort *m_serialPort = nullptr;
+ QByteArray m_readData;
QTextStream m_standardOutput;
- QTimer m_timer;
+ QTimer m_timer;
};
-#endif
+#endif // SERIALPORTREADER_H