QtNetwork (examples) - update the fortune server example
Unlike client, this needs a bit more changes: - remove redundant (and outdated) stdlib.h includes - use QRandomGenerator (instead of qsrand/qrand pair) - replace QStringList with QVector<QString> - Q_NULLPTR->nullptr, ExplicitType * -> auto - fix some weird indentation Task-number: QTBUG-60628 Change-Id: I12eed12711b1e622407bd8ecd1afdf56a2cf2097 Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
parent
9e268185bb
commit
0f5c4ba7cb
@ -1,6 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
**
|
**
|
||||||
** Copyright (C) 2016 The Qt Company Ltd.
|
** Copyright (C) 2017 The Qt Company Ltd.
|
||||||
** Contact: https://www.qt.io/licensing/
|
** Contact: https://www.qt.io/licensing/
|
||||||
**
|
**
|
||||||
** This file is part of the examples of the Qt Toolkit.
|
** This file is part of the examples of the Qt Toolkit.
|
||||||
@ -49,18 +49,14 @@
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QtCore>
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include "server.h"
|
#include "server.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
QGuiApplication::setApplicationDisplayName(Server::tr("Fortune Server"));
|
QApplication::setApplicationDisplayName(Server::tr("Fortune Server"));
|
||||||
Server server;
|
Server server;
|
||||||
server.show();
|
server.show();
|
||||||
qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
|
|
||||||
return app.exec();
|
return app.exec();
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
**
|
**
|
||||||
** Copyright (C) 2016 The Qt Company Ltd.
|
** Copyright (C) 2017 The Qt Company Ltd.
|
||||||
** Contact: https://www.qt.io/licensing/
|
** Contact: https://www.qt.io/licensing/
|
||||||
**
|
**
|
||||||
** This file is part of the examples of the Qt Toolkit.
|
** This file is part of the examples of the Qt Toolkit.
|
||||||
@ -50,16 +50,13 @@
|
|||||||
|
|
||||||
#include <QtWidgets>
|
#include <QtWidgets>
|
||||||
#include <QtNetwork>
|
#include <QtNetwork>
|
||||||
|
#include <QtCore>
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
#include "server.h"
|
#include "server.h"
|
||||||
|
|
||||||
Server::Server(QWidget *parent)
|
Server::Server(QWidget *parent)
|
||||||
: QDialog(parent)
|
: QDialog(parent)
|
||||||
, statusLabel(new QLabel)
|
, statusLabel(new QLabel)
|
||||||
, tcpServer(Q_NULLPTR)
|
|
||||||
, networkSession(0)
|
|
||||||
{
|
{
|
||||||
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
||||||
statusLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
|
statusLabel->setTextInteractionFlags(Qt::TextBrowserInteraction);
|
||||||
@ -97,25 +94,25 @@ Server::Server(QWidget *parent)
|
|||||||
<< tr("You cannot kill time without injuring eternity.")
|
<< tr("You cannot kill time without injuring eternity.")
|
||||||
<< tr("Computers are not intelligent. They only think they are.");
|
<< tr("Computers are not intelligent. They only think they are.");
|
||||||
//! [2]
|
//! [2]
|
||||||
QPushButton *quitButton = new QPushButton(tr("Quit"));
|
auto quitButton = new QPushButton(tr("Quit"));
|
||||||
quitButton->setAutoDefault(false);
|
quitButton->setAutoDefault(false);
|
||||||
connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close);
|
connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close);
|
||||||
//! [3]
|
//! [3]
|
||||||
connect(tcpServer, &QTcpServer::newConnection, this, &Server::sendFortune);
|
connect(tcpServer, &QTcpServer::newConnection, this, &Server::sendFortune);
|
||||||
//! [3]
|
//! [3]
|
||||||
|
|
||||||
QHBoxLayout *buttonLayout = new QHBoxLayout;
|
auto buttonLayout = new QHBoxLayout;
|
||||||
buttonLayout->addStretch(1);
|
buttonLayout->addStretch(1);
|
||||||
buttonLayout->addWidget(quitButton);
|
buttonLayout->addWidget(quitButton);
|
||||||
buttonLayout->addStretch(1);
|
buttonLayout->addStretch(1);
|
||||||
|
|
||||||
QVBoxLayout *mainLayout = Q_NULLPTR;
|
QVBoxLayout *mainLayout = nullptr;
|
||||||
if (QGuiApplication::styleHints()->showIsFullScreen() || QGuiApplication::styleHints()->showIsMaximized()) {
|
if (QGuiApplication::styleHints()->showIsFullScreen() || QGuiApplication::styleHints()->showIsMaximized()) {
|
||||||
QVBoxLayout *outerVerticalLayout = new QVBoxLayout(this);
|
auto outerVerticalLayout = new QVBoxLayout(this);
|
||||||
outerVerticalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
|
outerVerticalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
|
||||||
QHBoxLayout *outerHorizontalLayout = new QHBoxLayout;
|
auto outerHorizontalLayout = new QHBoxLayout;
|
||||||
outerHorizontalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Ignored));
|
outerHorizontalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Ignored));
|
||||||
QGroupBox *groupBox = new QGroupBox(QGuiApplication::applicationDisplayName());
|
auto groupBox = new QGroupBox(QGuiApplication::applicationDisplayName());
|
||||||
mainLayout = new QVBoxLayout(groupBox);
|
mainLayout = new QVBoxLayout(groupBox);
|
||||||
outerHorizontalLayout->addWidget(groupBox);
|
outerHorizontalLayout->addWidget(groupBox);
|
||||||
outerHorizontalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Ignored));
|
outerHorizontalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Ignored));
|
||||||
@ -183,9 +180,9 @@ void Server::sendFortune()
|
|||||||
//! [5]
|
//! [5]
|
||||||
QByteArray block;
|
QByteArray block;
|
||||||
QDataStream out(&block, QIODevice::WriteOnly);
|
QDataStream out(&block, QIODevice::WriteOnly);
|
||||||
out.setVersion(QDataStream::Qt_4_0);
|
out.setVersion(QDataStream::Qt_5_10);
|
||||||
|
|
||||||
out << fortunes.at(qrand() % fortunes.size());
|
out << fortunes[QRandomGenerator::bounded(fortunes.size())];
|
||||||
//! [4] //! [7]
|
//! [4] //! [7]
|
||||||
|
|
||||||
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
|
QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
**
|
**
|
||||||
** Copyright (C) 2016 The Qt Company Ltd.
|
** Copyright (C) 2017 The Qt Company Ltd.
|
||||||
** Contact: https://www.qt.io/licensing/
|
** Contact: https://www.qt.io/licensing/
|
||||||
**
|
**
|
||||||
** This file is part of the examples of the Qt Toolkit.
|
** This file is part of the examples of the Qt Toolkit.
|
||||||
@ -52,10 +52,11 @@
|
|||||||
#define SERVER_H
|
#define SERVER_H
|
||||||
|
|
||||||
#include <QDialog>
|
#include <QDialog>
|
||||||
|
#include <QString>
|
||||||
|
#include <QVector>
|
||||||
|
|
||||||
QT_BEGIN_NAMESPACE
|
QT_BEGIN_NAMESPACE
|
||||||
class QLabel;
|
class QLabel;
|
||||||
class QPushButton;
|
|
||||||
class QTcpServer;
|
class QTcpServer;
|
||||||
class QNetworkSession;
|
class QNetworkSession;
|
||||||
QT_END_NAMESPACE
|
QT_END_NAMESPACE
|
||||||
@ -66,17 +67,17 @@ class Server : public QDialog
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Server(QWidget *parent = Q_NULLPTR);
|
explicit Server(QWidget *parent = nullptr);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void sessionOpened();
|
void sessionOpened();
|
||||||
void sendFortune();
|
void sendFortune();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QLabel *statusLabel;
|
QLabel *statusLabel = nullptr;
|
||||||
QTcpServer *tcpServer;
|
QTcpServer *tcpServer = nullptr;
|
||||||
QStringList fortunes;
|
QVector<QString> fortunes;
|
||||||
QNetworkSession *networkSession;
|
QNetworkSession *networkSession = nullptr;
|
||||||
};
|
};
|
||||||
//! [0]
|
//! [0]
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user