QtNetwork (examples) - update broadcastreceiver too

... as soon as we updated the 'sender' example. Changes are purely cosmetic
though - 'explicit', nullptrs and local variables instead of redundant
data-members (QPushButton).

Task-number: QTBUG-60628
Change-Id: I572219da9d2a0ced07d94efb6f52f00b5a9c546d
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Timur Pocheptsov 2017-09-25 12:08:21 +02:00
parent 1402f5d16e
commit 9d2cdb163f
2 changed files with 11 additions and 14 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.
@ -59,7 +59,7 @@ Receiver::Receiver(QWidget *parent)
statusLabel = new QLabel(tr("Listening for broadcasted messages")); statusLabel = new QLabel(tr("Listening for broadcasted messages"));
statusLabel->setWordWrap(true); statusLabel->setWordWrap(true);
quitButton = new QPushButton(tr("&Quit")); auto quitButton = new QPushButton(tr("&Quit"));
//! [0] //! [0]
udpSocket = new QUdpSocket(this); udpSocket = new QUdpSocket(this);
@ -72,12 +72,12 @@ Receiver::Receiver(QWidget *parent)
//! [1] //! [1]
connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
QHBoxLayout *buttonLayout = new QHBoxLayout; auto buttonLayout = new QHBoxLayout;
buttonLayout->addStretch(1); buttonLayout->addStretch(1);
buttonLayout->addWidget(quitButton); buttonLayout->addWidget(quitButton);
buttonLayout->addStretch(1); buttonLayout->addStretch(1);
QVBoxLayout *mainLayout = new QVBoxLayout; auto mainLayout = new QVBoxLayout;
mainLayout->addWidget(statusLabel); mainLayout->addWidget(statusLabel);
mainLayout->addLayout(buttonLayout); mainLayout->addLayout(buttonLayout);
setLayout(mainLayout); setLayout(mainLayout);
@ -87,13 +87,13 @@ Receiver::Receiver(QWidget *parent)
void Receiver::processPendingDatagrams() void Receiver::processPendingDatagrams()
{ {
QByteArray datagram;
//! [2] //! [2]
while (udpSocket->hasPendingDatagrams()) { while (udpSocket->hasPendingDatagrams()) {
QByteArray datagram; datagram.resize(int(udpSocket->pendingDatagramSize()));
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(), datagram.size()); udpSocket->readDatagram(datagram.data(), datagram.size());
statusLabel->setText(tr("Received datagram: \"%1\"") statusLabel->setText(tr("Received datagram: \"%1\"")
.arg(datagram.data())); .arg(datagram.constData()));
} }
//! [2] //! [2]
} }

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.
@ -55,9 +55,7 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QLabel; class QLabel;
class QPushButton;
class QUdpSocket; class QUdpSocket;
class QAction;
QT_END_NAMESPACE QT_END_NAMESPACE
class Receiver : public QWidget class Receiver : public QWidget
@ -65,15 +63,14 @@ class Receiver : public QWidget
Q_OBJECT Q_OBJECT
public: public:
Receiver(QWidget *parent = 0); explicit Receiver(QWidget *parent = nullptr);
private slots: private slots:
void processPendingDatagrams(); void processPendingDatagrams();
private: private:
QLabel *statusLabel; QLabel *statusLabel = nullptr;
QPushButton *quitButton; QUdpSocket *udpSocket = nullptr;
QUdpSocket *udpSocket;
}; };
#endif #endif