summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Shienkov <denis.shienkov@gmail.com>2017-09-27 15:41:25 (GMT)
committerDenis Shienkov <denis.shienkov@gmail.com>2017-10-05 06:44:06 (GMT)
commitad8c10bd7a91e3bdda1a9df92ee307318780cdf4 (patch)
tree145736a0988e41c25c55e79b7c3a1e6459409b5a
parent2cc09578947ee65dc502d5097d4553b696d91c9b (diff)
downloadqtserialport-ad8c10bd7a91e3bdda1a9df92ee307318780cdf4.tar.gz
Revamp the Non-Blocking Slave Example
* 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: I3f7f078aad6d86f56feb6a3dfa78bf158ee0ee63 Reviewed-by: André Hartmann <aha_1980@gmx.de> Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r--examples/serialport/slave/dialog.cpp117
-rw-r--r--examples/serialport/slave/dialog.h31
-rw-r--r--examples/serialport/slave/main.cpp4
3 files changed, 73 insertions, 79 deletions
diff --git a/examples/serialport/slave/dialog.cpp b/examples/serialport/slave/dialog.cpp
index d801052..eec7999 100644
--- a/examples/serialport/slave/dialog.cpp
+++ b/examples/serialport/slave/dialog.cpp
@@ -50,109 +50,106 @@
#include "dialog.h"
+#include <QComboBox>
+#include <QGridLayout>
#include <QLabel>
#include <QLineEdit>
-#include <QComboBox>
-#include <QSpinBox>
#include <QPushButton>
-#include <QGridLayout>
+#include <QSerialPortInfo>
+#include <QSpinBox>
-#include <QtSerialPort/QSerialPortInfo>
-
-QT_USE_NAMESPACE
-
-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(20);
+ m_waitRequestSpinBox->setRange(0, 10000);
+ m_waitRequestSpinBox->setValue(20);
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("Slave"));
- serialPortComboBox->setFocus();
+ m_serialPortComboBox->setFocus();
- timer.setSingleShot(true);
+ m_timer.setSingleShot(true);
- connect(runButton, &QPushButton::clicked, this, &Dialog::startSlave);
- connect(&serial, &QSerialPort::readyRead, this, &Dialog::readRequest);
- connect(&timer, &QTimer::timeout, this, &Dialog::processTimeout);
+ connect(m_runButton, &QPushButton::clicked, this, &Dialog::startSlave);
+ connect(&m_serial, &QSerialPort::readyRead, this, &Dialog::readRequest);
+ connect(&m_timer, &QTimer::timeout, this, &Dialog::processTimeout);
- connect(serialPortComboBox, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
+ connect(m_serialPortComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
this, &Dialog::activateRunButton);
- connect(waitRequestSpinBox, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged),
+ connect(m_waitRequestSpinBox, QOverload<int>::of(&QSpinBox::valueChanged),
this, &Dialog::activateRunButton);
- connect(responseLineEdit, &QLineEdit::textChanged, this, &Dialog::activateRunButton);
+ connect(m_responseLineEdit, &QLineEdit::textChanged, this, &Dialog::activateRunButton);
}
void Dialog::startSlave()
{
- if (serial.portName() != serialPortComboBox->currentText()) {
- serial.close();
- serial.setPortName(serialPortComboBox->currentText());
+ if (m_serial.portName() != m_serialPortComboBox->currentText()) {
+ m_serial.close();
+ m_serial.setPortName(m_serialPortComboBox->currentText());
- if (!serial.open(QIODevice::ReadWrite)) {
+ if (!m_serial.open(QIODevice::ReadWrite)) {
processError(tr("Can't open %1, error code %2")
- .arg(serial.portName()).arg(serial.error()));
+ .arg(m_serial.portName()).arg(m_serial.error()));
return;
}
}
- runButton->setEnabled(false);
- statusLabel->setText(tr("Status: Running, connected to port %1.")
- .arg(serialPortComboBox->currentText()));
+ m_runButton->setEnabled(false);
+ m_statusLabel->setText(tr("Status: Running, connected to port %1.")
+ .arg(m_serialPortComboBox->currentText()));
}
void Dialog::readRequest()
{
- if (!timer.isActive())
- timer.start(waitRequestSpinBox->value());
- request.append(serial.readAll());
+ if (!m_timer.isActive())
+ m_timer.start(m_waitRequestSpinBox->value());
+ m_request.append(m_serial.readAll());
}
void Dialog::processTimeout()
{
- serial.write(responseLineEdit->text().toLocal8Bit());
-
- trafficLabel->setText(tr("Traffic, transaction #%1:"
- "\n\r-request: %2"
- "\n\r-response: %3")
- .arg(++transactionCount).arg(QString(request)).arg(responseLineEdit->text()));
- request.clear();
+ m_serial.write(m_responseLineEdit->text().toUtf8());
+
+ m_trafficLabel->setText(tr("Traffic, transaction #%1:"
+ "\n\r-request: %2"
+ "\n\r-response: %3")
+ .arg(++m_transactionCount).arg(QString::fromUtf8(m_request))
+ .arg(m_responseLineEdit->text()));
+ m_request.clear();
}
void Dialog::activateRunButton()
{
- runButton->setEnabled(true);
+ m_runButton->setEnabled(true);
}
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."));
}
diff --git a/examples/serialport/slave/dialog.h b/examples/serialport/slave/dialog.h
index 0405cf8..13909c7 100644
--- a/examples/serialport/slave/dialog.h
+++ b/examples/serialport/slave/dialog.h
@@ -52,12 +52,9 @@
#define DIALOG_H
#include <QDialog>
+#include <QSerialPort>
#include <QTimer>
-#include <QtSerialPort/QSerialPort>
-
-QT_USE_NAMESPACE
-
QT_BEGIN_NAMESPACE
class QLabel;
@@ -85,20 +82,20 @@ private:
void processError(const QString &s);
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;
- QSerialPort serial;
- QByteArray request;
- QTimer timer;
+ QSerialPort m_serial;
+ QByteArray m_request;
+ QTimer m_timer;
};
#endif // DIALOG_H
diff --git a/examples/serialport/slave/main.cpp b/examples/serialport/slave/main.cpp
index cf4c421..8e68c73 100644
--- a/examples/serialport/slave/main.cpp
+++ b/examples/serialport/slave/main.cpp
@@ -48,10 +48,10 @@
**
****************************************************************************/
-#include <QApplication>
-
#include "dialog.h"
+#include <QApplication>
+
int main(int argc, char *argv[])
{
QApplication app(argc, argv);