QtNetwork (examples) - update broadcastsender

Fix copyrights, update signal-slot connection syntax, use some simple
C++11 features (member-initializers, 'auto'), delete some data-members
(where a local variable is enough), where possible - use data-memebrs
as sub-objects (instead of heap allocated).

Task-number: QTBUG-60628
Change-Id: Ia440d8471eafb47481c0d010175c907037bae841
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Timur Pocheptsov 2017-09-20 15:56:31 +02:00
parent aa4ff7b2e9
commit ffbe848770
2 changed files with 18 additions and 23 deletions

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2016 The Qt Company Ltd. ** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/ ** Contact: https://www.qt.io/licensing/
** **
** This file is part of the examples of the Qt Toolkit. ** This file is part of the examples of the Qt Toolkit.
@ -50,6 +50,7 @@
#include <QtWidgets> #include <QtWidgets>
#include <QtNetwork> #include <QtNetwork>
#include <QtCore>
#include "sender.h" #include "sender.h"
@ -60,23 +61,21 @@ Sender::Sender(QWidget *parent)
statusLabel->setWordWrap(true); statusLabel->setWordWrap(true);
startButton = new QPushButton(tr("&Start")); 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(startButton, QDialogButtonBox::ActionRole);
buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
timer = new QTimer(this);
//! [0] //! [0]
udpSocket = new QUdpSocket(this); udpSocket = new QUdpSocket(this);
//! [0] //! [0]
messageNo = 1;
connect(startButton, SIGNAL(clicked()), this, SLOT(startBroadcasting())); connect(startButton, &QPushButton::clicked, this, &Sender::startBroadcasting);
connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); connect(quitButton, &QPushButton::clicked, this, &Sender::close);
connect(timer, SIGNAL(timeout()), this, SLOT(broadcastDatagram())); connect(&timer, &QTimer::timeout, this, &Sender::broadcastDatagram);
QVBoxLayout *mainLayout = new QVBoxLayout; auto mainLayout = new QVBoxLayout;
mainLayout->addWidget(statusLabel); mainLayout->addWidget(statusLabel);
mainLayout->addWidget(buttonBox); mainLayout->addWidget(buttonBox);
setLayout(mainLayout); setLayout(mainLayout);
@ -87,7 +86,7 @@ Sender::Sender(QWidget *parent)
void Sender::startBroadcasting() void Sender::startBroadcasting()
{ {
startButton->setEnabled(false); startButton->setEnabled(false);
timer->start(1000); timer.start(1000);
} }
void Sender::broadcastDatagram() void Sender::broadcastDatagram()
@ -95,8 +94,7 @@ void Sender::broadcastDatagram()
statusLabel->setText(tr("Now broadcasting datagram %1").arg(messageNo)); statusLabel->setText(tr("Now broadcasting datagram %1").arg(messageNo));
//! [1] //! [1]
QByteArray datagram = "Broadcast message " + QByteArray::number(messageNo); QByteArray datagram = "Broadcast message " + QByteArray::number(messageNo);
udpSocket->writeDatagram(datagram.data(), datagram.size(), udpSocket->writeDatagram(datagram, QHostAddress::Broadcast, 45454);
QHostAddress::Broadcast, 45454);
//! [1] //! [1]
++messageNo; ++messageNo;
} }

View File

@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
** **
** Copyright (C) 2016 The Qt Company Ltd. ** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/ ** Contact: https://www.qt.io/licensing/
** **
** This file is part of the examples of the Qt Toolkit. ** This file is part of the examples of the Qt Toolkit.
@ -52,12 +52,11 @@
#define SENDER_H #define SENDER_H
#include <QWidget> #include <QWidget>
#include <QTimer>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QDialogButtonBox;
class QLabel; class QLabel;
class QPushButton; class QPushButton;
class QTimer;
class QUdpSocket; class QUdpSocket;
QT_END_NAMESPACE QT_END_NAMESPACE
@ -66,20 +65,18 @@ class Sender : public QWidget
Q_OBJECT Q_OBJECT
public: public:
Sender(QWidget *parent = 0); explicit Sender(QWidget *parent = nullptr);
private slots: private slots:
void startBroadcasting(); void startBroadcasting();
void broadcastDatagram(); void broadcastDatagram();
private: private:
QLabel *statusLabel; QLabel *statusLabel = nullptr;
QPushButton *startButton; QPushButton *startButton = nullptr;
QPushButton *quitButton; QUdpSocket *udpSocket = nullptr;
QDialogButtonBox *buttonBox; QTimer timer;
QUdpSocket *udpSocket; int messageNo = 1;
QTimer *timer;
int messageNo;
}; };
#endif #endif