mirror of
https://github.com/crystalidea/qt6windows7.git
synced 2025-07-02 07:15:27 +08:00
qt 6.5.1 original
This commit is contained in:
40
examples/network/threadedfortuneserver/CMakeLists.txt
Normal file
40
examples/network/threadedfortuneserver/CMakeLists.txt
Normal file
@ -0,0 +1,40 @@
|
||||
# Copyright (C) 2022 The Qt Company Ltd.
|
||||
# SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(threadedfortuneserver LANGUAGES CXX)
|
||||
|
||||
if(NOT DEFINED INSTALL_EXAMPLESDIR)
|
||||
set(INSTALL_EXAMPLESDIR "examples")
|
||||
endif()
|
||||
|
||||
set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/network/threadedfortuneserver")
|
||||
|
||||
find_package(Qt6 REQUIRED COMPONENTS Core Gui Network Widgets)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
qt_add_executable(threadedfortuneserver
|
||||
dialog.cpp dialog.h
|
||||
fortuneserver.cpp fortuneserver.h
|
||||
fortunethread.cpp fortunethread.h
|
||||
main.cpp
|
||||
)
|
||||
|
||||
set_target_properties(threadedfortuneserver PROPERTIES
|
||||
WIN32_EXECUTABLE TRUE
|
||||
MACOSX_BUNDLE TRUE
|
||||
)
|
||||
|
||||
target_link_libraries(threadedfortuneserver PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Network
|
||||
Qt6::Widgets
|
||||
)
|
||||
|
||||
install(TARGETS threadedfortuneserver
|
||||
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
|
||||
)
|
57
examples/network/threadedfortuneserver/dialog.cpp
Normal file
57
examples/network/threadedfortuneserver/dialog.cpp
Normal file
@ -0,0 +1,57 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QtNetwork>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "dialog.h"
|
||||
#include "fortuneserver.h"
|
||||
|
||||
Dialog::Dialog(QWidget *parent)
|
||||
: QWidget(parent)
|
||||
{
|
||||
statusLabel = new QLabel;
|
||||
statusLabel->setWordWrap(true);
|
||||
quitButton = new QPushButton(tr("Quit"));
|
||||
quitButton->setAutoDefault(false);
|
||||
|
||||
if (!server.listen()) {
|
||||
QMessageBox::critical(this, tr("Threaded Fortune Server"),
|
||||
tr("Unable to start the server: %1.")
|
||||
.arg(server.errorString()));
|
||||
close();
|
||||
return;
|
||||
}
|
||||
|
||||
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(server.serverPort()));
|
||||
|
||||
connect(quitButton, &QPushButton::clicked, this, &Dialog::close);
|
||||
|
||||
QHBoxLayout *buttonLayout = new QHBoxLayout;
|
||||
buttonLayout->addStretch(1);
|
||||
buttonLayout->addWidget(quitButton);
|
||||
buttonLayout->addStretch(1);
|
||||
|
||||
QVBoxLayout *mainLayout = new QVBoxLayout;
|
||||
mainLayout->addWidget(statusLabel);
|
||||
mainLayout->addLayout(buttonLayout);
|
||||
setLayout(mainLayout);
|
||||
setWindowTitle(tr("Threaded Fortune Server"));
|
||||
}
|
28
examples/network/threadedfortuneserver/dialog.h
Normal file
28
examples/network/threadedfortuneserver/dialog.h
Normal file
@ -0,0 +1,28 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef DIALOG_H
|
||||
#define DIALOG_H
|
||||
|
||||
#include <QWidget>
|
||||
#include "fortuneserver.h"
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QLabel;
|
||||
class QPushButton;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
class Dialog : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
Dialog(QWidget *parent = nullptr);
|
||||
|
||||
private:
|
||||
QLabel *statusLabel;
|
||||
QPushButton *quitButton;
|
||||
FortuneServer server;
|
||||
};
|
||||
|
||||
#endif
|
33
examples/network/threadedfortuneserver/fortuneserver.cpp
Normal file
33
examples/network/threadedfortuneserver/fortuneserver.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "fortuneserver.h"
|
||||
#include "fortunethread.h"
|
||||
|
||||
#include <QRandomGenerator>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
//! [0]
|
||||
FortuneServer::FortuneServer(QObject *parent)
|
||||
: QTcpServer(parent)
|
||||
{
|
||||
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.");
|
||||
}
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
void FortuneServer::incomingConnection(qintptr socketDescriptor)
|
||||
{
|
||||
QString fortune = fortunes.at(QRandomGenerator::global()->bounded(fortunes.size()));
|
||||
FortuneThread *thread = new FortuneThread(socketDescriptor, fortune, this);
|
||||
connect(thread, &FortuneThread::finished, thread, &FortuneThread::deleteLater);
|
||||
thread->start();
|
||||
}
|
||||
//! [1]
|
26
examples/network/threadedfortuneserver/fortuneserver.h
Normal file
26
examples/network/threadedfortuneserver/fortuneserver.h
Normal file
@ -0,0 +1,26 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef FORTUNESERVER_H
|
||||
#define FORTUNESERVER_H
|
||||
|
||||
#include <QStringList>
|
||||
#include <QTcpServer>
|
||||
|
||||
//! [0]
|
||||
class FortuneServer : public QTcpServer
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
FortuneServer(QObject *parent = nullptr);
|
||||
|
||||
protected:
|
||||
void incomingConnection(qintptr socketDescriptor) override;
|
||||
|
||||
private:
|
||||
QStringList fortunes;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif
|
36
examples/network/threadedfortuneserver/fortunethread.cpp
Normal file
36
examples/network/threadedfortuneserver/fortunethread.cpp
Normal file
@ -0,0 +1,36 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include "fortunethread.h"
|
||||
|
||||
#include <QtNetwork>
|
||||
|
||||
//! [0]
|
||||
FortuneThread::FortuneThread(qintptr socketDescriptor, const QString &fortune, QObject *parent)
|
||||
: QThread(parent), socketDescriptor(socketDescriptor), text(fortune)
|
||||
{
|
||||
}
|
||||
//! [0]
|
||||
|
||||
//! [1]
|
||||
void FortuneThread::run()
|
||||
{
|
||||
QTcpSocket tcpSocket;
|
||||
//! [1] //! [2]
|
||||
if (!tcpSocket.setSocketDescriptor(socketDescriptor)) {
|
||||
emit error(tcpSocket.error());
|
||||
return;
|
||||
}
|
||||
//! [2] //! [3]
|
||||
|
||||
QByteArray block;
|
||||
QDataStream out(&block, QIODevice::WriteOnly);
|
||||
out.setVersion(QDataStream::Qt_6_5);
|
||||
out << text;
|
||||
//! [3] //! [4]
|
||||
|
||||
tcpSocket.write(block);
|
||||
tcpSocket.disconnectFromHost();
|
||||
tcpSocket.waitForDisconnected();
|
||||
}
|
||||
//! [4]
|
29
examples/network/threadedfortuneserver/fortunethread.h
Normal file
29
examples/network/threadedfortuneserver/fortunethread.h
Normal file
@ -0,0 +1,29 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#ifndef FORTUNETHREAD_H
|
||||
#define FORTUNETHREAD_H
|
||||
|
||||
#include <QThread>
|
||||
#include <QTcpSocket>
|
||||
|
||||
//! [0]
|
||||
class FortuneThread : public QThread
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
FortuneThread(qintptr socketDescriptor, const QString &fortune, QObject *parent);
|
||||
|
||||
void run() override;
|
||||
|
||||
signals:
|
||||
void error(QTcpSocket::SocketError socketError);
|
||||
|
||||
private:
|
||||
qintptr socketDescriptor;
|
||||
QString text;
|
||||
};
|
||||
//! [0]
|
||||
|
||||
#endif
|
17
examples/network/threadedfortuneserver/main.cpp
Normal file
17
examples/network/threadedfortuneserver/main.cpp
Normal file
@ -0,0 +1,17 @@
|
||||
// Copyright (C) 2016 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
||||
|
||||
#include <QApplication>
|
||||
#include <QtCore>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "dialog.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
Dialog dialog;
|
||||
dialog.show();
|
||||
return app.exec();
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
QT += network widgets
|
||||
|
||||
HEADERS = dialog.h \
|
||||
fortuneserver.h \
|
||||
fortunethread.h
|
||||
SOURCES = dialog.cpp \
|
||||
fortuneserver.cpp \
|
||||
fortunethread.cpp \
|
||||
main.cpp
|
||||
|
||||
# install
|
||||
target.path = $$[QT_INSTALL_EXAMPLES]/network/threadedfortuneserver
|
||||
INSTALLS += target
|
||||
|
||||
|
Reference in New Issue
Block a user