mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-03 15:55:27 +08:00
qt 6.5.1 original
This commit is contained in:
38
examples/network/broadcastreceiver/CMakeLists.txt
Normal file
38
examples/network/broadcastreceiver/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(broadcastreceiver LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/network/broadcastreceiver")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Network Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(broadcastreceiver
|
||||
main.cpp
|
||||
receiver.cpp receiver.h
|
||||
)
|
||||
|
||||
set_target_properties(broadcastreceiver PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(broadcastreceiver PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Network
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
install(TARGETS broadcastreceiver
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
11
examples/network/broadcastreceiver/broadcastreceiver.pro
Normal file
11
examples/network/broadcastreceiver/broadcastreceiver.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/broadcastreceiver
|
||||
INSTALLS += target
|
||||
|
14
examples/network/broadcastreceiver/main.cpp
Normal file
14
examples/network/broadcastreceiver/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();
|
||||
}
|
56
examples/network/broadcastreceiver/receiver.cpp
Normal file
56
examples/network/broadcastreceiver/receiver.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
// Copyright (C) 2017 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QLabel>
|
||||
#include <QPushButton>
|
||||
#include <QUdpSocket>
|
||||
#include <QVBoxLayout>
|
||||
#include <QCoreApplication>
|
||||
|
||||
#include "receiver.h"
|
||||
|
||||
Receiver::Receiver(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
statusLabel = new QLabel(tr("Listening for broadcasted messages"));
|
||||
statusLabel->setWordWrap(true);
|
||||
|
||||
auto quitButton = new QPushButton(tr("&Quit"));
|
||||
|
||||
//! [0]
|
||||
udpSocket = new QUdpSocket(this);
|
||||
udpSocket->bind(45454, QUdpSocket::ShareAddress);
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
connect(udpSocket, &QUdpSocket::readyRead,
|
||||
this, &Receiver::processPendingDatagrams);
|
||||
//! [1]
|
||||
connect(quitButton, &QPushButton::clicked,
|
||||
qApp, &QCoreApplication::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("Broadcast Receiver"));
|
||||
}
|
||||
|
||||
void Receiver::processPendingDatagrams()
|
||||
{
|
||||
QByteArray datagram;
|
||||
//! [2]
|
||||
while (udpSocket->hasPendingDatagrams()) {
|
||||
datagram.resize(int(udpSocket->pendingDatagramSize()));
|
||||
udpSocket->readDatagram(datagram.data(), datagram.size());
|
||||
statusLabel->setText(tr("Received datagram: \"%1\"")
|
||||
.arg(datagram.constData()));
|
||||
}
|
||||
//! [2]
|
||||
}
|
29
examples/network/broadcastreceiver/receiver.h
Normal file
29
examples/network/broadcastreceiver/receiver.h
Normal file
@ -0,0 +1,29 @@
|
||||
// Copyright (C) 2017 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef RECEIVER_H
|
||||
#define RECEIVER_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QLabel;
|
||||
class QUdpSocket;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class Receiver : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit Receiver(QWidget *parent = nullptr);
|
||||
|
||||
private slots:
|
||||
void processPendingDatagrams();
|
||||
|
||||
private:
|
||||
QLabel *statusLabel = nullptr;
|
||||
QUdpSocket *udpSocket = nullptr;
|
||||
};
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user