From 0f5c4ba7cbd628a29d3e84219834c20c29cd6741 Mon Sep 17 00:00:00 2001 From: Timur Pocheptsov Date: Wed, 27 Sep 2017 12:32:34 +0200 Subject: QtNetwork (examples) - update the fortune server example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 - Q_NULLPTR->nullptr, ExplicitType * -> auto - fix some weird indentation Task-number: QTBUG-60628 Change-Id: I12eed12711b1e622407bd8ecd1afdf56a2cf2097 Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Edward Welbourne --- examples/network/fortuneserver/main.cpp | 8 +--- examples/network/fortuneserver/server.cpp | 79 +++++++++++++++---------------- examples/network/fortuneserver/server.h | 15 +++--- 3 files changed, 48 insertions(+), 54 deletions(-) diff --git a/examples/network/fortuneserver/main.cpp b/examples/network/fortuneserver/main.cpp index 12137d647c..a64fcb26dc 100644 --- a/examples/network/fortuneserver/main.cpp +++ b/examples/network/fortuneserver/main.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. @@ -49,18 +49,14 @@ ****************************************************************************/ #include -#include - -#include #include "server.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); - QGuiApplication::setApplicationDisplayName(Server::tr("Fortune Server")); + QApplication::setApplicationDisplayName(Server::tr("Fortune Server")); Server server; server.show(); - qsrand(QTime(0,0,0).secsTo(QTime::currentTime())); return app.exec(); } diff --git a/examples/network/fortuneserver/server.cpp b/examples/network/fortuneserver/server.cpp index f027d68dd9..3915a73bf0 100644 --- a/examples/network/fortuneserver/server.cpp +++ b/examples/network/fortuneserver/server.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. @@ -50,16 +50,13 @@ #include #include - -#include +#include #include "server.h" Server::Server(QWidget *parent) : QDialog(parent) , statusLabel(new QLabel) - , tcpServer(Q_NULLPTR) - , networkSession(0) { setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); statusLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); @@ -89,46 +86,46 @@ Server::Server(QWidget *parent) } //! [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."); + 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] - QPushButton *quitButton = new QPushButton(tr("Quit")); - quitButton->setAutoDefault(false); - connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close); + auto quitButton = new QPushButton(tr("Quit")); + quitButton->setAutoDefault(false); + connect(quitButton, &QAbstractButton::clicked, this, &QWidget::close); //! [3] - connect(tcpServer, &QTcpServer::newConnection, this, &Server::sendFortune); + connect(tcpServer, &QTcpServer::newConnection, this, &Server::sendFortune); //! [3] - QHBoxLayout *buttonLayout = new QHBoxLayout; - buttonLayout->addStretch(1); - buttonLayout->addWidget(quitButton); - buttonLayout->addStretch(1); - - QVBoxLayout *mainLayout = Q_NULLPTR; - if (QGuiApplication::styleHints()->showIsFullScreen() || QGuiApplication::styleHints()->showIsMaximized()) { - QVBoxLayout *outerVerticalLayout = new QVBoxLayout(this); - outerVerticalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding)); - QHBoxLayout *outerHorizontalLayout = new QHBoxLayout; - outerHorizontalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Ignored)); - QGroupBox *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); - } + 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); + mainLayout->addWidget(statusLabel); + mainLayout->addLayout(buttonLayout); - setWindowTitle(QGuiApplication::applicationDisplayName()); + setWindowTitle(QGuiApplication::applicationDisplayName()); } void Server::sessionOpened() @@ -183,9 +180,9 @@ void Server::sendFortune() //! [5] QByteArray block; 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] QTcpSocket *clientConnection = tcpServer->nextPendingConnection(); diff --git a/examples/network/fortuneserver/server.h b/examples/network/fortuneserver/server.h index ea5ed78292..c5bfa7d928 100644 --- a/examples/network/fortuneserver/server.h +++ b/examples/network/fortuneserver/server.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2017 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the examples of the Qt Toolkit. @@ -52,10 +52,11 @@ #define SERVER_H #include +#include +#include QT_BEGIN_NAMESPACE class QLabel; -class QPushButton; class QTcpServer; class QNetworkSession; QT_END_NAMESPACE @@ -66,17 +67,17 @@ class Server : public QDialog Q_OBJECT public: - explicit Server(QWidget *parent = Q_NULLPTR); + explicit Server(QWidget *parent = nullptr); private slots: void sessionOpened(); void sendFortune(); private: - QLabel *statusLabel; - QTcpServer *tcpServer; - QStringList fortunes; - QNetworkSession *networkSession; + QLabel *statusLabel = nullptr; + QTcpServer *tcpServer = nullptr; + QVector fortunes; + QNetworkSession *networkSession = nullptr; }; //! [0] -- cgit v1.2.3