diff options
author | Timur Pocheptsov <timur.pocheptsov@qt.io> | 2017-09-27 12:32:34 +0200 |
---|---|---|
committer | Timur Pocheptsov <timur.pocheptsov@qt.io> | 2017-09-28 14:07:26 +0000 |
commit | 0f5c4ba7cbd628a29d3e84219834c20c29cd6741 (patch) | |
tree | 4d7d8eff06a2990641a555e134cea0cc4d16791c /examples/network | |
parent | 9e268185bbb39285ee087868f9d904d7c9f6fe61 (diff) | |
download | qtbase-0f5c4ba7cbd628a29d3e84219834c20c29cd6741.tar.gz |
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>
Diffstat (limited to 'examples/network')
-rw-r--r-- | examples/network/fortuneserver/main.cpp | 8 | ||||
-rw-r--r-- | examples/network/fortuneserver/server.cpp | 79 | ||||
-rw-r--r-- | 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 <QApplication> -#include <QtCore> - -#include <stdlib.h> #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 <QtWidgets> #include <QtNetwork> - -#include <stdlib.h> +#include <QtCore> #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 <QDialog> +#include <QString> +#include <QVector> 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<QString> fortunes; + QNetworkSession *networkSession = nullptr; }; //! [0] |