2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2017 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2019-11-01 12:21:32 +00:00
|
|
|
#include <QLabel>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QUdpSocket>
|
|
|
|
#include <QVBoxLayout>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#include "receiver.h"
|
|
|
|
|
|
|
|
Receiver::Receiver(QWidget *parent)
|
2011-04-27 17:16:41 +00:00
|
|
|
: QWidget(parent)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
statusLabel = new QLabel(tr("Listening for broadcasted messages"));
|
2011-04-27 17:16:41 +00:00
|
|
|
statusLabel->setWordWrap(true);
|
|
|
|
|
2017-09-25 10:08:21 +00:00
|
|
|
auto quitButton = new QPushButton(tr("&Quit"));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
//! [0]
|
|
|
|
udpSocket = new QUdpSocket(this);
|
|
|
|
udpSocket->bind(45454, QUdpSocket::ShareAddress);
|
|
|
|
//! [0]
|
|
|
|
|
|
|
|
//! [1]
|
2019-11-01 12:21:32 +00:00
|
|
|
connect(udpSocket, &QUdpSocket::readyRead,
|
|
|
|
this, &Receiver::processPendingDatagrams);
|
2011-04-27 10:05:43 +00:00
|
|
|
//! [1]
|
2019-11-01 12:21:32 +00:00
|
|
|
connect(quitButton, &QPushButton::clicked,
|
|
|
|
this, &Receiver::close);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-09-25 10:08:21 +00:00
|
|
|
auto buttonLayout = new QHBoxLayout;
|
2011-04-27 10:05:43 +00:00
|
|
|
buttonLayout->addStretch(1);
|
|
|
|
buttonLayout->addWidget(quitButton);
|
|
|
|
buttonLayout->addStretch(1);
|
|
|
|
|
2017-09-25 10:08:21 +00:00
|
|
|
auto mainLayout = new QVBoxLayout;
|
2011-04-27 10:05:43 +00:00
|
|
|
mainLayout->addWidget(statusLabel);
|
|
|
|
mainLayout->addLayout(buttonLayout);
|
|
|
|
setLayout(mainLayout);
|
|
|
|
|
|
|
|
setWindowTitle(tr("Broadcast Receiver"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Receiver::processPendingDatagrams()
|
|
|
|
{
|
2017-09-25 10:08:21 +00:00
|
|
|
QByteArray datagram;
|
2011-04-27 10:05:43 +00:00
|
|
|
//! [2]
|
|
|
|
while (udpSocket->hasPendingDatagrams()) {
|
2017-09-25 10:08:21 +00:00
|
|
|
datagram.resize(int(udpSocket->pendingDatagramSize()));
|
2011-04-27 10:05:43 +00:00
|
|
|
udpSocket->readDatagram(datagram.data(), datagram.size());
|
|
|
|
statusLabel->setText(tr("Received datagram: \"%1\"")
|
2017-09-25 10:08:21 +00:00
|
|
|
.arg(datagram.constData()));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
//! [2]
|
|
|
|
}
|