summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Shienkov <denis.shienkov@gmail.com>2017-09-27 15:50:11 (GMT)
committerDenis Shienkov <denis.shienkov@gmail.com>2017-10-05 06:44:18 (GMT)
commit2098f98997b82358ede6994f2e1d0a27ade30b1e (patch)
treeb47a71337d4b7f859570360fa622b15d72c07b8a
parentad8c10bd7a91e3bdda1a9df92ee307318780cdf4 (diff)
downloadqtserialport-2098f98997b82358ede6994f2e1d0a27ade30b1e.tar.gz
Revamp the Non-Blocking Master Example
* Use the 'm_' prefixes for all class members. * 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: Ibdc607653d8727b88d4e041d0a9eb0a96e3289d7 Reviewed-by: André Hartmann <aha_1980@gmx.de> Reviewed-by: Alex Blasche <alexander.blasche@qt.io>
-rw-r--r--examples/serialport/master/dialog.cpp113
-rw-r--r--examples/serialport/master/dialog.h31
-rw-r--r--examples/serialport/master/main.cpp4
3 files changed, 72 insertions, 76 deletions
diff --git a/examples/serialport/master/dialog.cpp b/examples/serialport/master/dialog.cpp
index 4bed08b..e12d837 100644
--- a/examples/serialport/master/dialog.cpp
+++ b/examples/serialport/master/dialog.cpp
@@ -50,106 +50,105 @@
#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())
- , 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_transactionCount(0),
+ 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(100);
+ m_waitResponseSpinBox->setRange(0, 10000);
+ m_waitResponseSpinBox->setValue(100);
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("Master"));
- serialPortComboBox->setFocus();
+ m_serialPortComboBox->setFocus();
- timer.setSingleShot(true);
+ m_timer.setSingleShot(true);
- connect(runButton, &QPushButton::clicked, this, &Dialog::sendRequest);
- connect(&serial, &QSerialPort::readyRead, this, &Dialog::readResponse);
- connect(&timer, &QTimer::timeout, this, &Dialog::processTimeout);
+ connect(m_runButton, &QPushButton::clicked, this, &Dialog::sendRequest);
+ connect(&m_serial, &QSerialPort::readyRead, this, &Dialog::readResponse);
+ connect(&m_timer, &QTimer::timeout, this, &Dialog::processTimeout);
}
void Dialog::sendRequest()
{
- 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;
}
}
setControlsEnabled(false);
- statusLabel->setText(tr("Status: Running, connected to port %1.")
- .arg(serialPortComboBox->currentText()));
+ m_statusLabel->setText(tr("Status: Running, connected to port %1.")
+ .arg(m_serialPortComboBox->currentText()));
- serial.write(requestLineEdit->text().toLocal8Bit());
- timer.start(waitResponseSpinBox->value());
+ m_serial.write(m_requestLineEdit->text().toUtf8());
+ m_timer.start(m_waitResponseSpinBox->value());
}
void Dialog::readResponse()
{
- response.append(serial.readAll());
+ m_response.append(m_serial.readAll());
}
void Dialog::processTimeout()
{
setControlsEnabled(true);
- trafficLabel->setText(tr("Traffic, transaction #%1:"
- "\n\r-request: %2"
- "\n\r-response: %3")
- .arg(++transactionCount).arg(requestLineEdit->text()).arg(QString(response)));
- response.clear();
+ m_trafficLabel->setText(tr("Traffic, transaction #%1:"
+ "\n\r-request: %2"
+ "\n\r-response: %3")
+ .arg(++m_transactionCount)
+ .arg(m_requestLineEdit->text())
+ .arg(QString::fromUtf8(m_response)));
+ m_response.clear();
}
void Dialog::processError(const QString &error)
{
setControlsEnabled(true);
- statusLabel->setText(tr("Status: Not running, %1.").arg(error));
- trafficLabel->setText(tr("No traffic."));
+ m_statusLabel->setText(tr("Status: Not running, %1.").arg(error));
+ 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/master/dialog.h b/examples/serialport/master/dialog.h
index 0bc1241..2fb2e8e 100644
--- a/examples/serialport/master/dialog.h
+++ b/examples/serialport/master/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 &error);
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;
- QSerialPort serial;
- QByteArray response;
- QTimer timer;
+ QSerialPort m_serial;
+ QByteArray m_response;
+ QTimer m_timer;
};
#endif // DIALOG_H
diff --git a/examples/serialport/master/main.cpp b/examples/serialport/master/main.cpp
index cf4c421..8e68c73 100644
--- a/examples/serialport/master/main.cpp
+++ b/examples/serialport/master/main.cpp
@@ -48,10 +48,10 @@
**
****************************************************************************/
-#include <QApplication>
-
#include "dialog.h"
+#include <QApplication>
+
int main(int argc, char *argv[])
{
QApplication app(argc, argv);