mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-02 15:26:00 +08:00
qt 6.5.1 original
This commit is contained in:
38
examples/network/multicastreceiver/CMakeLists.txt
Normal file
38
examples/network/multicastreceiver/CMakeLists.txt
Normal file
@ -0,0 +1,38 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(multicastreceiver LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/network/multicastreceiver")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Network Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(multicastreceiver
|
||||
main.cpp
|
||||
receiver.cpp receiver.h
|
||||
)
|
||||
|
||||
set_target_properties(multicastreceiver PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(multicastreceiver PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Network
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
install(TARGETS multicastreceiver
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
14
examples/network/multicastreceiver/main.cpp
Normal file
14
examples/network/multicastreceiver/main.cpp
Normal file
@ -0,0 +1,14 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
#include "receiver.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
Receiver receiver;
|
||||
receiver.show();
|
||||
return app.exec();
|
||||
}
|
11
examples/network/multicastreceiver/multicastreceiver.pro
Normal file
11
examples/network/multicastreceiver/multicastreceiver.pro
Normal file
@ -0,0 +1,11 @@
|
||||
QT += network widgets
|
||||
requires(qtConfig(udpsocket))
|
||||
|
||||
HEADERS = receiver.h
|
||||
SOURCES = receiver.cpp \
|
||||
main.cpp
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/network/multicastreceiver
|
||||
INSTALLS += target
|
||||
|
64
examples/network/multicastreceiver/receiver.cpp
Normal file
64
examples/network/multicastreceiver/receiver.cpp
Normal file
@ -0,0 +1,64 @@
|
||||
// Copyright (C) 2017 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QtNetwork>
|
||||
|
||||
#include "receiver.h"
|
||||
|
||||
Receiver::Receiver(QWidget *parent)
|
||||
: QDialog(parent),
|
||||
groupAddress4(QStringLiteral("239.255.43.21")),
|
||||
groupAddress6(QStringLiteral("ff12::2115"))
|
||||
{
|
||||
statusLabel = new QLabel(tr("Listening for multicast messages on both IPv4 and IPv6"));
|
||||
auto quitButton = new QPushButton(tr("&Quit"));
|
||||
|
||||
auto buttonLayout = new QHBoxLayout;
|
||||
buttonLayout->addStretch(1);
|
||||
buttonLayout->addWidget(quitButton);
|
||||
buttonLayout->addStretch(1);
|
||||
|
||||
auto mainLayout = new QVBoxLayout;
|
||||
mainLayout->addWidget(statusLabel);
|
||||
mainLayout->addLayout(buttonLayout);
|
||||
setLayout(mainLayout);
|
||||
|
||||
setWindowTitle(tr("Multicast Receiver"));
|
||||
|
||||
udpSocket4.bind(QHostAddress::AnyIPv4, 45454, QUdpSocket::ShareAddress);
|
||||
udpSocket4.joinMulticastGroup(groupAddress4);
|
||||
|
||||
if (!udpSocket6.bind(QHostAddress::AnyIPv6, 45454, QUdpSocket::ShareAddress) ||
|
||||
!udpSocket6.joinMulticastGroup(groupAddress6))
|
||||
statusLabel->setText(tr("Listening for multicast messages on IPv4 only"));
|
||||
|
||||
connect(&udpSocket4, &QUdpSocket::readyRead,
|
||||
this, &Receiver::processPendingDatagrams);
|
||||
connect(&udpSocket6, &QUdpSocket::readyRead,
|
||||
this, &Receiver::processPendingDatagrams);
|
||||
connect(quitButton, &QPushButton::clicked,
|
||||
qApp, &QCoreApplication::quit);
|
||||
}
|
||||
|
||||
void Receiver::processPendingDatagrams()
|
||||
{
|
||||
QByteArray datagram;
|
||||
|
||||
// using QUdpSocket::readDatagram (API since Qt 4)
|
||||
while (udpSocket4.hasPendingDatagrams()) {
|
||||
datagram.resize(qsizetype(udpSocket4.pendingDatagramSize()));
|
||||
udpSocket4.readDatagram(datagram.data(), datagram.size());
|
||||
statusLabel->setText(tr("Received IPv4 datagram: \"%1\"")
|
||||
.arg(datagram.constData()));
|
||||
}
|
||||
|
||||
// using QUdpSocket::receiveDatagram (API since Qt 5.8)
|
||||
while (udpSocket6.hasPendingDatagrams()) {
|
||||
QNetworkDatagram dgram = udpSocket6.receiveDatagram();
|
||||
statusLabel->setText(statusLabel->text() +
|
||||
tr("\nReceived IPv6 datagram from [%2]:%3: \"%1\"")
|
||||
.arg(dgram.data().constData(), dgram.senderAddress().toString())
|
||||
.arg(dgram.senderPort()));
|
||||
}
|
||||
}
|
33
examples/network/multicastreceiver/receiver.h
Normal file
33
examples/network/multicastreceiver/receiver.h
Normal file
@ -0,0 +1,33 @@
|
||||
// Copyright (C) 2017 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef RECEIVER_H
|
||||
#define RECEIVER_H
|
||||
|
||||
#include <QDialog>
|
||||
#include <QHostAddress>
|
||||
#include <QUdpSocket>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QLabel;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class Receiver : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit Receiver(QWidget *parent = nullptr);
|
||||
|
||||
private slots:
|
||||
void processPendingDatagrams();
|
||||
|
||||
private:
|
||||
QLabel *statusLabel = nullptr;
|
||||
QUdpSocket udpSocket4;
|
||||
QUdpSocket udpSocket6;
|
||||
QHostAddress groupAddress4;
|
||||
QHostAddress groupAddress6;
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user