From 07e1fc7bb93dc4c306d8e64f4cb88d42177cba2e Mon Sep 17 00:00:00 2001 From: Denis Shienkov Date: Wed, 27 Sep 2017 19:56:10 +0300 Subject: Revamp the Blocking Master 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. * 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 --- .../serialport/blockingmaster/masterthread.cpp | 87 +++++++++++----------- 1 file changed, 42 insertions(+), 45 deletions(-) (limited to 'examples/serialport/blockingmaster/masterthread.cpp') 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 - +#include #include -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] } -- cgit v1.2.1