From 503920ac9e5b43615561555544367df5cc2ee1e8 Mon Sep 17 00:00:00 2001 From: Andre Hartmann Date: Sat, 28 Jul 2018 23:24:43 +0200 Subject: Examples: Revamp Loopback * order and sort includes * use functor connect * use nullptr * use member init * added sanity check for nextPendingConnection() * small cleanup here and there Change-Id: I72c6758b5fedea0937a1f2cb9031cb7203f5d955 Reviewed-by: Christian Ehrlicher Reviewed-by: Timur Pocheptsov --- examples/network/loopback/dialog.cpp | 55 ++++++++++++++++++++---------------- examples/network/loopback/dialog.h | 27 ++++++++---------- examples/network/loopback/main.cpp | 4 +-- 3 files changed, 45 insertions(+), 41 deletions(-) diff --git a/examples/network/loopback/dialog.cpp b/examples/network/loopback/dialog.cpp index 99317b9c77..b4e6b0fd5e 100644 --- a/examples/network/loopback/dialog.cpp +++ b/examples/network/loopback/dialog.cpp @@ -48,11 +48,11 @@ ** ****************************************************************************/ -#include -#include - #include "dialog.h" +#include +#include + static const int TotalBytes = 50 * 1024 * 1024; static const int PayloadSize = 64 * 1024; // 64 KB @@ -71,15 +71,15 @@ Dialog::Dialog(QWidget *parent) buttonBox->addButton(startButton, QDialogButtonBox::ActionRole); buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); - connect(startButton, SIGNAL(clicked()), this, SLOT(start())); - connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); - connect(&tcpServer, SIGNAL(newConnection()), - this, SLOT(acceptConnection())); - connect(&tcpClient, SIGNAL(connected()), this, SLOT(startTransfer())); - connect(&tcpClient, SIGNAL(bytesWritten(qint64)), - this, SLOT(updateClientProgress(qint64))); - connect(&tcpClient, SIGNAL(error(QAbstractSocket::SocketError)), - this, SLOT(displayError(QAbstractSocket::SocketError))); + connect(startButton, &QAbstractButton::clicked, this, &Dialog::start); + connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close); + connect(&tcpServer, &QTcpServer::newConnection, + this, &Dialog::acceptConnection); + connect(&tcpClient, &QAbstractSocket::connected, this, &Dialog::startTransfer); + connect(&tcpClient, &QIODevice::bytesWritten, + this, &Dialog::updateClientProgress); + connect(&tcpClient, QOverload::of(&QAbstractSocket::error), + this, &Dialog::displayError); QVBoxLayout *mainLayout = new QVBoxLayout; mainLayout->addWidget(clientProgressBar); @@ -124,10 +124,18 @@ void Dialog::start() void Dialog::acceptConnection() { tcpServerConnection = tcpServer.nextPendingConnection(); - connect(tcpServerConnection, SIGNAL(readyRead()), - this, SLOT(updateServerProgress())); - connect(tcpServerConnection, SIGNAL(error(QAbstractSocket::SocketError)), - this, SLOT(displayError(QAbstractSocket::SocketError))); + if (!tcpServerConnection) { + serverStatusLabel->setText(tr("Error: got invalid pending connection!")); + return; + } + + connect(tcpServerConnection, &QIODevice::readyRead, + this, &Dialog::updateServerProgress); + connect(tcpServerConnection, + QOverload::of(&QAbstractSocket::error), + this, &Dialog::displayError); + connect(tcpServerConnection, &QTcpSocket::disconnected, + tcpServerConnection, &QTcpSocket::deleteLater); serverStatusLabel->setText(tr("Accepted connection")); tcpServer.close(); @@ -136,13 +144,13 @@ void Dialog::acceptConnection() void Dialog::startTransfer() { // called when the TCP client connected to the loopback server - bytesToWrite = TotalBytes - (int)tcpClient.write(QByteArray(PayloadSize, '@')); + bytesToWrite = TotalBytes - int(tcpClient.write(QByteArray(PayloadSize, '@'))); clientStatusLabel->setText(tr("Connected")); } void Dialog::updateServerProgress() { - bytesReceived += (int)tcpServerConnection->bytesAvailable(); + bytesReceived += int(tcpServerConnection->bytesAvailable()); tcpServerConnection->readAll(); serverProgressBar->setMaximum(TotalBytes); @@ -161,17 +169,16 @@ void Dialog::updateServerProgress() void Dialog::updateClientProgress(qint64 numBytes) { - // callen when the TCP client has written some bytes - bytesWritten += (int)numBytes; + // called when the TCP client has written some bytes + bytesWritten += int(numBytes); // only write more if not finished and when the Qt write buffer is below a certain size. - if (bytesToWrite > 0 && tcpClient.bytesToWrite() <= 4*PayloadSize) - bytesToWrite -= (int)tcpClient.write(QByteArray(qMin(bytesToWrite, PayloadSize), '@')); + if (bytesToWrite > 0 && tcpClient.bytesToWrite() <= 4 * PayloadSize) + bytesToWrite -= tcpClient.write(QByteArray(qMin(bytesToWrite, PayloadSize), '@')); clientProgressBar->setMaximum(TotalBytes); clientProgressBar->setValue(bytesWritten); - clientStatusLabel->setText(tr("Sent %1MB") - .arg(bytesWritten / (1024 * 1024))); + clientStatusLabel->setText(tr("Sent %1MB").arg(bytesWritten / (1024 * 1024))); } void Dialog::displayError(QAbstractSocket::SocketError socketError) diff --git a/examples/network/loopback/dialog.h b/examples/network/loopback/dialog.h index b23cfa030b..a70c20550a 100644 --- a/examples/network/loopback/dialog.h +++ b/examples/network/loopback/dialog.h @@ -60,9 +60,6 @@ class QDialogButtonBox; class QLabel; class QProgressBar; class QPushButton; -class QTcpServer; -class QTcpSocket; -class QAction; QT_END_NAMESPACE class Dialog : public QDialog @@ -70,7 +67,7 @@ class Dialog : public QDialog Q_OBJECT public: - Dialog(QWidget *parent = 0); + Dialog(QWidget *parent = nullptr); public slots: void start(); @@ -81,21 +78,21 @@ public slots: void displayError(QAbstractSocket::SocketError socketError); private: - QProgressBar *clientProgressBar; - QProgressBar *serverProgressBar; - QLabel *clientStatusLabel; - QLabel *serverStatusLabel; + QProgressBar *clientProgressBar = nullptr; + QProgressBar *serverProgressBar = nullptr; + QLabel *clientStatusLabel = nullptr; + QLabel *serverStatusLabel = nullptr; - QPushButton *startButton; - QPushButton *quitButton; - QDialogButtonBox *buttonBox; + QPushButton *startButton = nullptr; + QPushButton *quitButton = nullptr; + QDialogButtonBox *buttonBox = nullptr; QTcpServer tcpServer; QTcpSocket tcpClient; - QTcpSocket *tcpServerConnection; - int bytesToWrite; - int bytesWritten; - int bytesReceived; + QTcpSocket *tcpServerConnection = nullptr; + int bytesToWrite = 0; + int bytesWritten = 0; + int bytesReceived = 0; }; #endif diff --git a/examples/network/loopback/main.cpp b/examples/network/loopback/main.cpp index 9959c472f4..094a11d4fc 100644 --- a/examples/network/loopback/main.cpp +++ b/examples/network/loopback/main.cpp @@ -48,10 +48,10 @@ ** ****************************************************************************/ -#include - #include "dialog.h" +#include + int main(int argc, char *argv[]) { QApplication app(argc, argv); -- cgit v1.2.3