From 466705128376bd5d60ec23eec7af37d3bf6649fe Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Wed, 27 Sep 2017 19:31:15 +0300 Subject: Revamp the Console Asynchronous Reader Example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 Reviewed-by: Alex Blasche --- examples/serialport/creaderasync/main.cpp | 26 ++++++++++-------- .../serialport/creaderasync/serialportreader.cpp | 32 ++++++++++++---------- .../serialport/creaderasync/serialportreader.h | 17 ++++-------- 3 files changed, 37 insertions(+), 38 deletions(-) (limited to 'examples') diff --git a/examples/serialport/creaderasync/main.cpp b/examples/serialport/creaderasync/main.cpp index dd1f7559..6c669ceb 100644 --- a/examples/serialport/creaderasync/main.cpp +++ b/examples/serialport/creaderasync/main.cpp @@ -50,37 +50,39 @@ #include "serialportreader.h" -#include - -#include #include -#include +#include #include - -QT_USE_NAMESPACE +#include 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 [baudrate]").arg(argumentList.first()) << endl; + standardOutput << QObject::tr("Usage: %1 [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 f85dd8a7..a701839b 100644 --- a/examples/serialport/creaderasync/serialportreader.cpp +++ b/examples/serialport/creaderasync/serialportreader.cpp @@ -52,25 +52,18 @@ #include -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(&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 66980877..53074f14 100644 --- a/examples/serialport/creaderasync/serialportreader.h +++ b/examples/serialport/creaderasync/serialportreader.h @@ -51,14 +51,10 @@ #ifndef SERIALPORTREADER_H #define SERIALPORTREADER_H -#include - +#include +#include #include #include -#include -#include - -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 -- cgit v1.2.3