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 <edward.welbourne@qt.io>
This commit is contained in:
parent
ddb191e47a
commit
a732e16d5f
@ -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 <QtWidgets>
|
||||
#include <QtNetwork>
|
||||
|
||||
#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, QOverload<int>::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);
|
||||
|
||||
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()));
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -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 <QDialog>
|
||||
#include <QHostAddress>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QDialogButtonBox;
|
||||
class QLabel;
|
||||
class QPushButton;
|
||||
class QTimer;
|
||||
class QUdpSocket;
|
||||
class QSpinBox;
|
||||
QT_END_NAMESPACE
|
||||
#include <QtWidgets>
|
||||
#include <QtNetwork>
|
||||
#include <QtCore>
|
||||
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user