mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-02 23:35:28 +08:00
qt 6.5.1 original
This commit is contained in:
38
examples/network/fortuneserver/CMakeLists.txt
Normal file
38
examples/network/fortuneserver/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(fortuneserver LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/network/fortuneserver")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Network Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(fortuneserver
|
||||
main.cpp
|
||||
server.cpp server.h
|
||||
)
|
||||
|
||||
set_target_properties(fortuneserver PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(fortuneserver PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Network
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
install(TARGETS fortuneserver
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
9
examples/network/fortuneserver/fortuneserver.pro
Normal file
9
examples/network/fortuneserver/fortuneserver.pro
Normal file
@ -0,0 +1,9 @@
|
||||
QT += network widgets
|
||||
|
||||
HEADERS = server.h
|
||||
SOURCES = server.cpp \
|
||||
main.cpp
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/network/fortuneserver
|
||||
INSTALLS += target
|
15
examples/network/fortuneserver/main.cpp
Normal file
15
examples/network/fortuneserver/main.cpp
Normal file
@ -0,0 +1,15 @@
|
||||
// Copyright (C) 2017 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
#include "server.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
QApplication::setApplicationDisplayName(Server::tr("Fortune Server"));
|
||||
Server server;
|
||||
server.show();
|
||||
return app.exec();
|
||||
}
|
111
examples/network/fortuneserver/server.cpp
Normal file
111
examples/network/fortuneserver/server.cpp
Normal file
@ -0,0 +1,111 @@
|
||||
// Copyright (C) 2017 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QtNetwork>
|
||||
#include <QtCore>
|
||||
|
||||
#include "server.h"
|
||||
|
||||
Server::Server(QWidget *parent)
|
||||
: QDialog(parent)
|
||||
, statusLabel(new QLabel)
|
||||
{
|
||||
statusLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
|
||||
|
||||
initServer();
|
||||
|
||||
//! [2]
|
||||
fortunes << tr("You've been leading a dog's life. Stay off the furniture.")
|
||||
<< tr("You've got to think about tomorrow.")
|
||||
<< tr("You will be surprised by a loud noise.")
|
||||
<< tr("You will feel hungry again in another hour.")
|
||||
<< tr("You might have mail.")
|
||||
<< tr("You cannot kill time without injuring eternity.")
|
||||
<< tr("Computers are not intelligent. They only think they are.");
|
||||
//! [2]
|
||||
auto quitButton = new QPushButton(tr("Quit"));
|
||||
quitButton->setAutoDefault(false);
|
||||
connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close);
|
||||
//! [3]
|
||||
connect(tcpServer, &QTcpServer::newConnection, this, &Server::sendFortune);
|
||||
//! [3]
|
||||
|
||||
auto buttonLayout = new QHBoxLayout;
|
||||
buttonLayout->addStretch(1);
|
||||
buttonLayout->addWidget(quitButton);
|
||||
buttonLayout->addStretch(1);
|
||||
|
||||
QVBoxLayout *mainLayout = nullptr;
|
||||
if (QGuiApplication::styleHints()->showIsFullScreen() || QGuiApplication::styleHints()->showIsMaximized()) {
|
||||
auto outerVerticalLayout = new QVBoxLayout(this);
|
||||
outerVerticalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
|
||||
auto outerHorizontalLayout = new QHBoxLayout;
|
||||
outerHorizontalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Ignored));
|
||||
auto groupBox = new QGroupBox(QGuiApplication::applicationDisplayName());
|
||||
mainLayout = new QVBoxLayout(groupBox);
|
||||
outerHorizontalLayout->addWidget(groupBox);
|
||||
outerHorizontalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Ignored));
|
||||
outerVerticalLayout->addLayout(outerHorizontalLayout);
|
||||
outerVerticalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
|
||||
} else {
|
||||
mainLayout = new QVBoxLayout(this);
|
||||
}
|
||||
|
||||
mainLayout->addWidget(statusLabel);
|
||||
mainLayout->addLayout(buttonLayout);
|
||||
|
||||
setWindowTitle(QGuiApplication::applicationDisplayName());
|
||||
}
|
||||
|
||||
void Server::initServer()
|
||||
{
|
||||
//! [0] //! [1]
|
||||
tcpServer = new QTcpServer(this);
|
||||
if (!tcpServer->listen()) {
|
||||
QMessageBox::critical(this, tr("Fortune Server"),
|
||||
tr("Unable to start the server: %1.")
|
||||
.arg(tcpServer->errorString()));
|
||||
close();
|
||||
return;
|
||||
}
|
||||
//! [0]
|
||||
QString ipAddress;
|
||||
const QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
|
||||
// use the first non-localhost IPv4 address
|
||||
for (const QHostAddress &entry : ipAddressesList) {
|
||||
if (entry != QHostAddress::LocalHost && entry.toIPv4Address()) {
|
||||
ipAddress = entry.toString();
|
||||
break;
|
||||
}
|
||||
}
|
||||
// if we did not find one, use IPv4 localhost
|
||||
if (ipAddress.isEmpty())
|
||||
ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
|
||||
statusLabel->setText(tr("The server is running on\n\nIP: %1\nport: %2\n\n"
|
||||
"Run the Fortune Client example now.")
|
||||
.arg(ipAddress).arg(tcpServer->serverPort()));
|
||||
//! [1]
|
||||
}
|
||||
|
||||
//! [4]
|
||||
void Server::sendFortune()
|
||||
{
|
||||
//! [5]
|
||||
QByteArray block;
|
||||
QDataStream out(&block, QIODevice::WriteOnly);
|
||||
out.setVersion(QDataStream::Qt_6_5);
|
||||
|
||||
out << fortunes[QRandomGenerator::global()->bounded(fortunes.size())];
|
||||
//! [4] //! [7]
|
||||
|
||||
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
|
||||
connect(clientConnection, &QAbstractSocket::disconnected,
|
||||
clientConnection, &QObject::deleteLater);
|
||||
//! [7] //! [8]
|
||||
|
||||
clientConnection->write(block);
|
||||
clientConnection->disconnectFromHost();
|
||||
//! [5]
|
||||
}
|
||||
//! [8]
|
36
examples/network/fortuneserver/server.h
Normal file
36
examples/network/fortuneserver/server.h
Normal file
@ -0,0 +1,36 @@
|
||||
// Copyright (C) 2017 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef SERVER_H
|
||||
#define SERVER_H
|
||||
|
||||
#include <QDialog>
|
||||
#include <QString>
|
||||
#include <QList>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QLabel;
|
||||
class QTcpServer;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
//! [0]
|
||||
class Server : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit Server(QWidget *parent = nullptr);
|
||||
|
||||
private slots:
|
||||
void sendFortune();
|
||||
|
||||
private:
|
||||
void initServer();
|
||||
|
||||
QLabel *statusLabel = nullptr;
|
||||
QTcpServer *tcpServer = nullptr;
|
||||
QList<QString> fortunes;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user