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:
parent
aa4ff7b2e9
commit
ffbe848770
@ -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.
|
||||
@ -50,6 +50,7 @@
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QtNetwork>
|
||||
#include <QtCore>
|
||||
|
||||
#include "sender.h"
|
||||
|
||||
@ -60,23 +61,21 @@ Sender::Sender(QWidget *parent)
|
||||
statusLabel->setWordWrap(true);
|
||||
|
||||
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);
|
||||
//! [0]
|
||||
udpSocket = new QUdpSocket(this);
|
||||
//! [0]
|
||||
messageNo = 1;
|
||||
|
||||
connect(startButton, SIGNAL(clicked()), this, SLOT(startBroadcasting()));
|
||||
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
|
||||
connect(timer, SIGNAL(timeout()), this, SLOT(broadcastDatagram()));
|
||||
connect(startButton, &QPushButton::clicked, this, &Sender::startBroadcasting);
|
||||
connect(quitButton, &QPushButton::clicked, this, &Sender::close);
|
||||
connect(&timer, &QTimer::timeout, this, &Sender::broadcastDatagram);
|
||||
|
||||
QVBoxLayout *mainLayout = new QVBoxLayout;
|
||||
auto mainLayout = new QVBoxLayout;
|
||||
mainLayout->addWidget(statusLabel);
|
||||
mainLayout->addWidget(buttonBox);
|
||||
setLayout(mainLayout);
|
||||
@ -87,7 +86,7 @@ Sender::Sender(QWidget *parent)
|
||||
void Sender::startBroadcasting()
|
||||
{
|
||||
startButton->setEnabled(false);
|
||||
timer->start(1000);
|
||||
timer.start(1000);
|
||||
}
|
||||
|
||||
void Sender::broadcastDatagram()
|
||||
@ -95,8 +94,7 @@ void Sender::broadcastDatagram()
|
||||
statusLabel->setText(tr("Now broadcasting datagram %1").arg(messageNo));
|
||||
//! [1]
|
||||
QByteArray datagram = "Broadcast message " + QByteArray::number(messageNo);
|
||||
udpSocket->writeDatagram(datagram.data(), datagram.size(),
|
||||
QHostAddress::Broadcast, 45454);
|
||||
udpSocket->writeDatagram(datagram, QHostAddress::Broadcast, 45454);
|
||||
//! [1]
|
||||
++messageNo;
|
||||
}
|
||||
|
@ -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.
|
||||
@ -52,12 +52,11 @@
|
||||
#define SENDER_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QTimer>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QDialogButtonBox;
|
||||
class QLabel;
|
||||
class QPushButton;
|
||||
class QTimer;
|
||||
class QUdpSocket;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
@ -66,20 +65,18 @@ class Sender : public QWidget
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Sender(QWidget *parent = 0);
|
||||
explicit Sender(QWidget *parent = nullptr);
|
||||
|
||||
private slots:
|
||||
void startBroadcasting();
|
||||
void broadcastDatagram();
|
||||
|
||||
private:
|
||||
QLabel *statusLabel;
|
||||
QPushButton *startButton;
|
||||
QPushButton *quitButton;
|
||||
QDialogButtonBox *buttonBox;
|
||||
QUdpSocket *udpSocket;
|
||||
QTimer *timer;
|
||||
int messageNo;
|
||||
QLabel *statusLabel = nullptr;
|
||||
QPushButton *startButton = nullptr;
|
||||
QUdpSocket *udpSocket = nullptr;
|
||||
QTimer timer;
|
||||
int messageNo = 1;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user