From a732e16d5fd9dbf8a0289fec9f948b12e9ba2c19 Mon Sep 17 00:00:00 2001 From: Timur Pocheptsov Date: Wed, 20 Sep 2017 16:34:21 +0200 Subject: QtNetwork (examples) - update multicastsender Similar to broadcast sender: - where possible use local variables instead of data-members - where possible avoid heap-allocated objects, use sub-objects instead - change signal-slot connection syntax (and as a "bonus" - show our QOverload) Task-number: QTBUG-60628 Change-Id: I8cd4f888c1d0653bdc8591800e713bbd347ad2fb Reviewed-by: Edward Welbourne --- examples/network/multicastsender/sender.cpp | 41 +++++++++++------------------ examples/network/multicastsender/sender.h | 32 +++++++--------------- 2 files changed, 26 insertions(+), 47 deletions(-) diff --git a/examples/network/multicastsender/sender.cpp b/examples/network/multicastsender/sender.cpp index 4aa65fee27..cb4bf45672 100644 --- a/examples/network/multicastsender/sender.cpp +++ b/examples/network/multicastsender/sender.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. @@ -48,43 +48,35 @@ ** ****************************************************************************/ -#include -#include - #include "sender.h" Sender::Sender(QWidget *parent) - : QDialog(parent) + : QDialog(parent), + groupAddress(QStringLiteral("239.255.43.21")) { - groupAddress = QHostAddress("239.255.43.21"); - statusLabel = new QLabel(tr("Ready to multicast datagrams to group %1 on port 45454").arg(groupAddress.toString())); - ttlLabel = new QLabel(tr("TTL for multicast datagrams:")); - ttlSpinBox = new QSpinBox; + auto ttlLabel = new QLabel(tr("TTL for multicast datagrams:")); + auto ttlSpinBox = new QSpinBox; ttlSpinBox->setRange(0, 255); - QHBoxLayout *ttlLayout = new QHBoxLayout; + auto ttlLayout = new QHBoxLayout; ttlLayout->addWidget(ttlLabel); ttlLayout->addWidget(ttlSpinBox); startButton = new QPushButton(tr("&Start")); - quitButton = new QPushButton(tr("&Quit")); + auto quitButton = new QPushButton(tr("&Quit")); - buttonBox = new QDialogButtonBox; + auto buttonBox = new QDialogButtonBox; buttonBox->addButton(startButton, QDialogButtonBox::ActionRole); buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); - timer = new QTimer(this); - udpSocket = new QUdpSocket(this); - messageNo = 1; - - connect(ttlSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ttlChanged(int))); - connect(startButton, SIGNAL(clicked()), this, SLOT(startSending())); - connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); - connect(timer, SIGNAL(timeout()), this, SLOT(sendDatagram())); + connect(ttlSpinBox, QOverload::of(&QSpinBox::valueChanged), this, &Sender::ttlChanged); + connect(startButton, &QPushButton::clicked, this, &Sender::startSending); + connect(quitButton, &QPushButton::clicked, this, &Sender::close); + connect(&timer, &QTimer::timeout, this, &Sender::sendDatagram); - QVBoxLayout *mainLayout = new QVBoxLayout; + auto mainLayout = new QVBoxLayout; mainLayout->addWidget(statusLabel); mainLayout->addLayout(ttlLayout); mainLayout->addWidget(buttonBox); @@ -96,20 +88,19 @@ Sender::Sender(QWidget *parent) void Sender::ttlChanged(int newTtl) { - udpSocket->setSocketOption(QAbstractSocket::MulticastTtlOption, newTtl); + udpSocket.setSocketOption(QAbstractSocket::MulticastTtlOption, newTtl); } void Sender::startSending() { startButton->setEnabled(false); - timer->start(1000); + timer.start(1000); } void Sender::sendDatagram() { statusLabel->setText(tr("Now sending datagram %1").arg(messageNo)); QByteArray datagram = "Multicast message " + QByteArray::number(messageNo); - udpSocket->writeDatagram(datagram.data(), datagram.size(), - groupAddress, 45454); + udpSocket.writeDatagram(datagram, groupAddress, 45454); ++messageNo; } diff --git a/examples/network/multicastsender/sender.h b/examples/network/multicastsender/sender.h index 8e10f88c0d..5d8769790e 100644 --- a/examples/network/multicastsender/sender.h +++ b/examples/network/multicastsender/sender.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. @@ -51,24 +51,16 @@ #ifndef SENDER_H #define SENDER_H -#include -#include - -QT_BEGIN_NAMESPACE -class QDialogButtonBox; -class QLabel; -class QPushButton; -class QTimer; -class QUdpSocket; -class QSpinBox; -QT_END_NAMESPACE +#include +#include +#include class Sender : public QDialog { Q_OBJECT public: - Sender(QWidget *parent = 0); + explicit Sender(QWidget *parent = nullptr); private slots: void ttlChanged(int newTtl); @@ -76,16 +68,12 @@ private slots: void sendDatagram(); private: - QLabel *statusLabel; - QLabel *ttlLabel; - QSpinBox *ttlSpinBox; - QPushButton *startButton; - QPushButton *quitButton; - QDialogButtonBox *buttonBox; - QUdpSocket *udpSocket; - QTimer *timer; + QLabel *statusLabel = nullptr; + QPushButton *startButton = nullptr; + QUdpSocket udpSocket; + QTimer timer; QHostAddress groupAddress; - int messageNo; + int messageNo = 1; }; #endif -- cgit v1.2.3