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/
|
** 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.
|
||||||
@ -48,43 +48,35 @@
|
|||||||
**
|
**
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include <QtWidgets>
|
|
||||||
#include <QtNetwork>
|
|
||||||
|
|
||||||
#include "sender.h"
|
#include "sender.h"
|
||||||
|
|
||||||
Sender::Sender(QWidget *parent)
|
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()));
|
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:"));
|
auto ttlLabel = new QLabel(tr("TTL for multicast datagrams:"));
|
||||||
ttlSpinBox = new QSpinBox;
|
auto ttlSpinBox = new QSpinBox;
|
||||||
ttlSpinBox->setRange(0, 255);
|
ttlSpinBox->setRange(0, 255);
|
||||||
|
|
||||||
QHBoxLayout *ttlLayout = new QHBoxLayout;
|
auto ttlLayout = new QHBoxLayout;
|
||||||
ttlLayout->addWidget(ttlLabel);
|
ttlLayout->addWidget(ttlLabel);
|
||||||
ttlLayout->addWidget(ttlSpinBox);
|
ttlLayout->addWidget(ttlSpinBox);
|
||||||
|
|
||||||
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);
|
connect(ttlSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), this, &Sender::ttlChanged);
|
||||||
udpSocket = new QUdpSocket(this);
|
connect(startButton, &QPushButton::clicked, this, &Sender::startSending);
|
||||||
messageNo = 1;
|
connect(quitButton, &QPushButton::clicked, this, &Sender::close);
|
||||||
|
connect(&timer, &QTimer::timeout, this, &Sender::sendDatagram);
|
||||||
|
|
||||||
connect(ttlSpinBox, SIGNAL(valueChanged(int)), this, SLOT(ttlChanged(int)));
|
auto mainLayout = new QVBoxLayout;
|
||||||
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;
|
|
||||||
mainLayout->addWidget(statusLabel);
|
mainLayout->addWidget(statusLabel);
|
||||||
mainLayout->addLayout(ttlLayout);
|
mainLayout->addLayout(ttlLayout);
|
||||||
mainLayout->addWidget(buttonBox);
|
mainLayout->addWidget(buttonBox);
|
||||||
@ -96,20 +88,19 @@ Sender::Sender(QWidget *parent)
|
|||||||
|
|
||||||
void Sender::ttlChanged(int newTtl)
|
void Sender::ttlChanged(int newTtl)
|
||||||
{
|
{
|
||||||
udpSocket->setSocketOption(QAbstractSocket::MulticastTtlOption, newTtl);
|
udpSocket.setSocketOption(QAbstractSocket::MulticastTtlOption, newTtl);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sender::startSending()
|
void Sender::startSending()
|
||||||
{
|
{
|
||||||
startButton->setEnabled(false);
|
startButton->setEnabled(false);
|
||||||
timer->start(1000);
|
timer.start(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Sender::sendDatagram()
|
void Sender::sendDatagram()
|
||||||
{
|
{
|
||||||
statusLabel->setText(tr("Now sending datagram %1").arg(messageNo));
|
statusLabel->setText(tr("Now sending datagram %1").arg(messageNo));
|
||||||
QByteArray datagram = "Multicast message " + QByteArray::number(messageNo);
|
QByteArray datagram = "Multicast message " + QByteArray::number(messageNo);
|
||||||
udpSocket->writeDatagram(datagram.data(), datagram.size(),
|
udpSocket.writeDatagram(datagram, groupAddress, 45454);
|
||||||
groupAddress, 45454);
|
|
||||||
++messageNo;
|
++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/
|
** 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.
|
||||||
@ -51,24 +51,16 @@
|
|||||||
#ifndef SENDER_H
|
#ifndef SENDER_H
|
||||||
#define SENDER_H
|
#define SENDER_H
|
||||||
|
|
||||||
#include <QDialog>
|
#include <QtWidgets>
|
||||||
#include <QHostAddress>
|
#include <QtNetwork>
|
||||||
|
#include <QtCore>
|
||||||
QT_BEGIN_NAMESPACE
|
|
||||||
class QDialogButtonBox;
|
|
||||||
class QLabel;
|
|
||||||
class QPushButton;
|
|
||||||
class QTimer;
|
|
||||||
class QUdpSocket;
|
|
||||||
class QSpinBox;
|
|
||||||
QT_END_NAMESPACE
|
|
||||||
|
|
||||||
class Sender : public QDialog
|
class Sender : public QDialog
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Sender(QWidget *parent = 0);
|
explicit Sender(QWidget *parent = nullptr);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void ttlChanged(int newTtl);
|
void ttlChanged(int newTtl);
|
||||||
@ -76,16 +68,12 @@ private slots:
|
|||||||
void sendDatagram();
|
void sendDatagram();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QLabel *statusLabel;
|
QLabel *statusLabel = nullptr;
|
||||||
QLabel *ttlLabel;
|
QPushButton *startButton = nullptr;
|
||||||
QSpinBox *ttlSpinBox;
|
QUdpSocket udpSocket;
|
||||||
QPushButton *startButton;
|
QTimer timer;
|
||||||
QPushButton *quitButton;
|
|
||||||
QDialogButtonBox *buttonBox;
|
|
||||||
QUdpSocket *udpSocket;
|
|
||||||
QTimer *timer;
|
|
||||||
QHostAddress groupAddress;
|
QHostAddress groupAddress;
|
||||||
int messageNo;
|
int messageNo = 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user