diff options
author | Liang Qi <liang.qi@qt.io> | 2017-10-17 10:19:31 +0200 |
---|---|---|
committer | Liang Qi <liang.qi@qt.io> | 2017-10-17 10:34:24 +0200 |
commit | d0a0a3c0418a0fdd2ed84b2a5f7e6565537715c6 (patch) | |
tree | d6aeb4d51caf30ccf23eadb806a09295cbf679cd /examples/network | |
parent | 9f405f98a4247cd263b9c5d35659a4ba89e282de (diff) | |
parent | ac35f9c44c0fb3b2f40ae5585c497200b2ba743d (diff) |
Merge remote-tracking branch 'origin/5.10' into dev
Conflicts:
examples/network/fortuneclient/client.cpp
examples/network/fortuneserver/server.cpp
src/platformsupport/platformcompositor/qopenglcompositorbackingstore_p.h
src/plugins/platforms/cocoa/qcocoabackingstore.h
src/plugins/platforms/cocoa/qcocoaintegration.h
src/plugins/platforms/cocoa/qcocoascreen.h
src/plugins/platforms/ios/qiosbackingstore.h
src/plugins/sqldrivers/oci/qsql_oci.cpp
src/widgets/kernel/qwidgetwindow.cpp
Change-Id: Ia6dd2c52d4a691b671cf9a2ffca70deccece8f10
Diffstat (limited to 'examples/network')
21 files changed, 235 insertions, 222 deletions
diff --git a/examples/network/broadcastreceiver/receiver.cpp b/examples/network/broadcastreceiver/receiver.cpp index 4225a19746..2f111b4795 100644 --- a/examples/network/broadcastreceiver/receiver.cpp +++ b/examples/network/broadcastreceiver/receiver.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. @@ -59,7 +59,7 @@ Receiver::Receiver(QWidget *parent) statusLabel = new QLabel(tr("Listening for broadcasted messages")); statusLabel->setWordWrap(true); - quitButton = new QPushButton(tr("&Quit")); + auto quitButton = new QPushButton(tr("&Quit")); //! [0] udpSocket = new QUdpSocket(this); @@ -72,12 +72,12 @@ Receiver::Receiver(QWidget *parent) //! [1] connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); - QHBoxLayout *buttonLayout = new QHBoxLayout; + auto buttonLayout = new QHBoxLayout; buttonLayout->addStretch(1); buttonLayout->addWidget(quitButton); buttonLayout->addStretch(1); - QVBoxLayout *mainLayout = new QVBoxLayout; + auto mainLayout = new QVBoxLayout; mainLayout->addWidget(statusLabel); mainLayout->addLayout(buttonLayout); setLayout(mainLayout); @@ -87,13 +87,13 @@ Receiver::Receiver(QWidget *parent) void Receiver::processPendingDatagrams() { + QByteArray datagram; //! [2] while (udpSocket->hasPendingDatagrams()) { - QByteArray datagram; - datagram.resize(udpSocket->pendingDatagramSize()); + datagram.resize(int(udpSocket->pendingDatagramSize())); udpSocket->readDatagram(datagram.data(), datagram.size()); statusLabel->setText(tr("Received datagram: \"%1\"") - .arg(datagram.data())); + .arg(datagram.constData())); } //! [2] } diff --git a/examples/network/broadcastreceiver/receiver.h b/examples/network/broadcastreceiver/receiver.h index 71b91246fc..e6f8d97c23 100644 --- a/examples/network/broadcastreceiver/receiver.h +++ b/examples/network/broadcastreceiver/receiver.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. @@ -55,9 +55,7 @@ QT_BEGIN_NAMESPACE class QLabel; -class QPushButton; class QUdpSocket; -class QAction; QT_END_NAMESPACE class Receiver : public QWidget @@ -65,15 +63,14 @@ class Receiver : public QWidget Q_OBJECT public: - Receiver(QWidget *parent = 0); + explicit Receiver(QWidget *parent = nullptr); private slots: void processPendingDatagrams(); private: - QLabel *statusLabel; - QPushButton *quitButton; - QUdpSocket *udpSocket; + QLabel *statusLabel = nullptr; + QUdpSocket *udpSocket = nullptr; }; #endif diff --git a/examples/network/download/main.cpp b/examples/network/download/main.cpp index e5ad050de3..96111983ea 100644 --- a/examples/network/download/main.cpp +++ b/examples/network/download/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. @@ -48,37 +48,29 @@ ** ****************************************************************************/ -#include <QCoreApplication> -#include <QFile> -#include <QFileInfo> -#include <QList> -#include <QNetworkAccessManager> -#include <QNetworkRequest> -#include <QNetworkReply> -#include <QSslError> -#include <QStringList> -#include <QTimer> -#include <QUrl> +#include <QtCore> +#include <QtNetwork> -#include <stdio.h> +#include <cstdio> QT_BEGIN_NAMESPACE class QSslError; QT_END_NAMESPACE -QT_USE_NAMESPACE +using namespace std; class DownloadManager: public QObject { Q_OBJECT QNetworkAccessManager manager; - QList<QNetworkReply *> currentDownloads; + QVector<QNetworkReply *> currentDownloads; public: DownloadManager(); void doDownload(const QUrl &url); - QString saveFileName(const QUrl &url); + static QString saveFileName(const QUrl &url); bool saveToDisk(const QString &filename, QIODevice *data); + static bool isHttpRedirect(QNetworkReply *reply); public slots: void execute(); @@ -97,8 +89,9 @@ void DownloadManager::doDownload(const QUrl &url) QNetworkRequest request(url); QNetworkReply *reply = manager.get(request); -#ifndef QT_NO_SSL - connect(reply, SIGNAL(sslErrors(QList<QSslError>)), SLOT(sslErrors(QList<QSslError>))); +#if QT_CONFIG(ssl) + connect(reply, SIGNAL(sslErrors(QList<QSslError>)), + SLOT(sslErrors(QList<QSslError>))); #endif currentDownloads.append(reply); @@ -141,6 +134,13 @@ bool DownloadManager::saveToDisk(const QString &filename, QIODevice *data) return true; } +bool DownloadManager::isHttpRedirect(QNetworkReply *reply) +{ + int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + return statusCode == 301 || statusCode == 302 || statusCode == 303 + || statusCode == 305 || statusCode == 307 || statusCode == 308; +} + void DownloadManager::execute() { QStringList args = QCoreApplication::instance()->arguments(); @@ -156,7 +156,7 @@ void DownloadManager::execute() return; } - foreach (QString arg, args) { + for (const QString &arg : qAsConst(args)) { QUrl url = QUrl::fromEncoded(arg.toLocal8Bit()); doDownload(url); } @@ -164,8 +164,8 @@ void DownloadManager::execute() void DownloadManager::sslErrors(const QList<QSslError> &sslErrors) { -#ifndef QT_NO_SSL - foreach (const QSslError &error, sslErrors) +#if QT_CONFIG(ssl) + for (const QSslError &error : sslErrors) fprintf(stderr, "SSL error: %s\n", qPrintable(error.errorString())); #else Q_UNUSED(sslErrors); @@ -180,18 +180,24 @@ void DownloadManager::downloadFinished(QNetworkReply *reply) url.toEncoded().constData(), qPrintable(reply->errorString())); } else { - QString filename = saveFileName(url); - if (saveToDisk(filename, reply)) - printf("Download of %s succeeded (saved to %s)\n", - url.toEncoded().constData(), qPrintable(filename)); + if (isHttpRedirect(reply)) { + fputs("Request was redirected.\n", stderr); + } else { + QString filename = saveFileName(url); + if (saveToDisk(filename, reply)) { + printf("Download of %s succeeded (saved to %s)\n", + url.toEncoded().constData(), qPrintable(filename)); + } + } } currentDownloads.removeAll(reply); reply->deleteLater(); - if (currentDownloads.isEmpty()) + if (currentDownloads.isEmpty()) { // all downloads finished QCoreApplication::instance()->quit(); + } } int main(int argc, char **argv) diff --git a/examples/network/downloadmanager/downloadmanager.cpp b/examples/network/downloadmanager/downloadmanager.cpp index 69d8fd1ebc..e820b4ff70 100644 --- a/examples/network/downloadmanager/downloadmanager.cpp +++ b/examples/network/downloadmanager/downloadmanager.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,23 +50,21 @@ #include "downloadmanager.h" -#include <QFileInfo> -#include <QNetworkRequest> -#include <QNetworkReply> -#include <QString> -#include <QStringList> -#include <QTimer> -#include <stdio.h> +#include <QTextStream> + +#include <cstdio> + +using namespace std; DownloadManager::DownloadManager(QObject *parent) - : QObject(parent), downloadedCount(0), totalCount(0) + : QObject(parent) { } -void DownloadManager::append(const QStringList &urlList) +void DownloadManager::append(const QStringList &urls) { - foreach (QString url, urlList) - append(QUrl::fromEncoded(url.toLocal8Bit())); + for (const QString &urlAsString : urls) + append(QUrl::fromEncoded(urlAsString.toLocal8Bit())); if (downloadQueue.isEmpty()) QTimer::singleShot(0, this, SIGNAL(finished())); @@ -167,9 +165,16 @@ void DownloadManager::downloadFinished() if (currentDownload->error()) { // download failed fprintf(stderr, "Failed: %s\n", qPrintable(currentDownload->errorString())); + output.remove(); } else { - printf("Succeeded.\n"); - ++downloadedCount; + // let's check if it was actually a redirect + if (isHttpRedirect()) { + reportRedirect(); + output.remove(); + } else { + printf("Succeeded.\n"); + ++downloadedCount; + } } currentDownload->deleteLater(); @@ -180,3 +185,28 @@ void DownloadManager::downloadReadyRead() { output.write(currentDownload->readAll()); } + +bool DownloadManager::isHttpRedirect() const +{ + int statusCode = currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + return statusCode == 301 || statusCode == 302 || statusCode == 303 + || statusCode == 305 || statusCode == 307 || statusCode == 308; +} + +void DownloadManager::reportRedirect() +{ + int statusCode = currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + QUrl requestUrl = currentDownload->request().url(); + QTextStream(stderr) << "Request: " << requestUrl.toDisplayString() + << " was redirected with code: " << statusCode + << '\n'; + + QVariant target = currentDownload->attribute(QNetworkRequest::RedirectionTargetAttribute); + if (!target.isValid()) + return; + QUrl redirectUrl = target.toUrl(); + if (redirectUrl.isRelative()) + redirectUrl = requestUrl.resolved(redirectUrl); + QTextStream(stderr) << "Redirected to: " << redirectUrl.toDisplayString() + << '\n'; +} diff --git a/examples/network/downloadmanager/downloadmanager.h b/examples/network/downloadmanager/downloadmanager.h index 550a197ef8..4bc6351ff9 100644 --- a/examples/network/downloadmanager/downloadmanager.h +++ b/examples/network/downloadmanager/downloadmanager.h @@ -51,12 +51,8 @@ #ifndef DOWNLOADMANAGER_H #define DOWNLOADMANAGER_H -#include <QFile> -#include <QObject> -#include <QQueue> -#include <QTime> -#include <QUrl> -#include <QNetworkAccessManager> +#include <QtNetwork> +#include <QtCore> #include "textprogressbar.h" @@ -64,11 +60,11 @@ class DownloadManager: public QObject { Q_OBJECT public: - DownloadManager(QObject *parent = 0); + explicit DownloadManager(QObject *parent = nullptr); void append(const QUrl &url); - void append(const QStringList &urlList); - QString saveFileName(const QUrl &url); + void append(const QStringList &urls); + static QString saveFileName(const QUrl &url); signals: void finished(); @@ -80,15 +76,18 @@ private slots: void downloadReadyRead(); private: + bool isHttpRedirect() const; + void reportRedirect(); + QNetworkAccessManager manager; QQueue<QUrl> downloadQueue; - QNetworkReply *currentDownload; + QNetworkReply *currentDownload = nullptr; QFile output; QTime downloadTime; TextProgressBar progressBar; - int downloadedCount; - int totalCount; + int downloadedCount = 0; + int totalCount = 0; }; #endif diff --git a/examples/network/downloadmanager/main.cpp b/examples/network/downloadmanager/main.cpp index 158f04c9e6..e3ba490992 100644 --- a/examples/network/downloadmanager/main.cpp +++ b/examples/network/downloadmanager/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. @@ -50,11 +50,15 @@ #include <QCoreApplication> #include <QStringList> + #include "downloadmanager.h" -#include <stdio.h> + +#include <cstdio> int main(int argc, char **argv) { + using namespace std; + QCoreApplication app(argc, argv); QStringList arguments = app.arguments(); arguments.takeFirst(); // remove the first argument, which is the program's name diff --git a/examples/network/downloadmanager/textprogressbar.cpp b/examples/network/downloadmanager/textprogressbar.cpp index d9506a563a..3449e6bad5 100644 --- a/examples/network/downloadmanager/textprogressbar.cpp +++ b/examples/network/downloadmanager/textprogressbar.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,22 +49,21 @@ ****************************************************************************/ #include "textprogressbar.h" + #include <QByteArray> -#include <stdio.h> -TextProgressBar::TextProgressBar() - : value(0), maximum(-1), iteration(0) -{ -} +#include <cstdio> + +using namespace std; void TextProgressBar::clear() { printf("\n"); fflush(stdout); - iteration = 0; value = 0; maximum = -1; + iteration = 0; } void TextProgressBar::update() diff --git a/examples/network/downloadmanager/textprogressbar.h b/examples/network/downloadmanager/textprogressbar.h index 57bcd6e92a..30affeb55c 100644 --- a/examples/network/downloadmanager/textprogressbar.h +++ b/examples/network/downloadmanager/textprogressbar.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. @@ -56,8 +56,6 @@ class TextProgressBar { public: - TextProgressBar(); - void clear(); void update(); void setMessage(const QString &message); @@ -65,9 +63,9 @@ public: private: QString message; - qint64 value; - qint64 maximum; - int iteration; + qint64 value = 0; + qint64 maximum = -1; + int iteration = 0; }; #endif diff --git a/examples/network/fortuneclient/client.cpp b/examples/network/fortuneclient/client.cpp index 3156a9d62c..4d3a318a7b 100644 --- a/examples/network/fortuneclient/client.cpp +++ b/examples/network/fortuneclient/client.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. @@ -60,7 +60,6 @@ Client::Client(QWidget *parent) , portLineEdit(new QLineEdit) , getFortuneButton(new QPushButton(tr("Get Fortune"))) , tcpSocket(new QTcpSocket(this)) - , networkSession(nullptr) { setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); //! [0] @@ -90,9 +89,9 @@ Client::Client(QWidget *parent) portLineEdit->setValidator(new QIntValidator(1, 65535, this)); - QLabel *hostLabel = new QLabel(tr("&Server name:")); + auto hostLabel = new QLabel(tr("&Server name:")); hostLabel->setBuddy(hostCombo); - QLabel *portLabel = new QLabel(tr("S&erver port:")); + auto portLabel = new QLabel(tr("S&erver port:")); portLabel->setBuddy(portLineEdit); statusLabel = new QLabel(tr("This examples requires that you run the " @@ -101,9 +100,9 @@ Client::Client(QWidget *parent) getFortuneButton->setDefault(true); getFortuneButton->setEnabled(false); - QPushButton *quitButton = new QPushButton(tr("Quit")); + auto quitButton = new QPushButton(tr("Quit")); - QDialogButtonBox *buttonBox = new QDialogButtonBox; + auto buttonBox = new QDialogButtonBox; buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole); buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); @@ -129,11 +128,11 @@ Client::Client(QWidget *parent) QGridLayout *mainLayout = nullptr; 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)); - QHBoxLayout *outerHorizontalLayout = new QHBoxLayout; + auto outerHorizontalLayout = new QHBoxLayout; outerHorizontalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Ignored)); - QGroupBox *groupBox = new QGroupBox(QGuiApplication::applicationDisplayName()); + auto groupBox = new QGroupBox(QGuiApplication::applicationDisplayName()); mainLayout = new QGridLayout(groupBox); outerHorizontalLayout->addWidget(groupBox); outerHorizontalLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::MinimumExpanding, QSizePolicy::Ignored)); diff --git a/examples/network/fortuneclient/client.h b/examples/network/fortuneclient/client.h index 3ba15173a4..ac335acb83 100644 --- a/examples/network/fortuneclient/client.h +++ b/examples/network/fortuneclient/client.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. @@ -51,9 +51,9 @@ #ifndef CLIENT_H #define CLIENT_H +#include <QDataStream> #include <QDialog> #include <QTcpSocket> -#include <QDataStream> QT_BEGIN_NAMESPACE class QComboBox; @@ -80,16 +80,16 @@ private slots: void sessionOpened(); private: - QComboBox *hostCombo; - QLineEdit *portLineEdit; - QLabel *statusLabel; - QPushButton *getFortuneButton; + QComboBox *hostCombo = nullptr; + QLineEdit *portLineEdit = nullptr; + QLabel *statusLabel = nullptr; + QPushButton *getFortuneButton = nullptr; - QTcpSocket *tcpSocket; + QTcpSocket *tcpSocket = nullptr; QDataStream in; QString currentFortune; - QNetworkSession *networkSession; + QNetworkSession *networkSession = nullptr; }; //! [0] diff --git a/examples/network/fortuneclient/main.cpp b/examples/network/fortuneclient/main.cpp index e313b251ef..6b02708ce1 100644 --- a/examples/network/fortuneclient/main.cpp +++ b/examples/network/fortuneclient/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. @@ -54,7 +54,7 @@ int main(int argc, char *argv[]) { QApplication app(argc, argv); - QGuiApplication::setApplicationDisplayName(Client::tr("Fortune Client")); + QApplication::setApplicationDisplayName(Client::tr("Fortune Client")); Client client; client.show(); return app.exec(); 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 aac95ad820..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(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 = 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 87cdea9c8c..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 @@ -73,10 +74,10 @@ private slots: void sendFortune(); private: - QLabel *statusLabel; - QTcpServer *tcpServer; - QStringList fortunes; - QNetworkSession *networkSession; + QLabel *statusLabel = nullptr; + QTcpServer *tcpServer = nullptr; + QVector<QString> fortunes; + QNetworkSession *networkSession = nullptr; }; //! [0] diff --git a/examples/network/googlesuggest/googlesuggest.cpp b/examples/network/googlesuggest/googlesuggest.cpp index 9fdfd8faa6..24fdde0a5c 100644 --- a/examples/network/googlesuggest/googlesuggest.cpp +++ b/examples/network/googlesuggest/googlesuggest.cpp @@ -51,7 +51,7 @@ //! [1] #include "googlesuggest.h" -#define GSUGGEST_URL "http://google.com/complete/search?output=toolbar&q=%1" +const QString gsuggestUrl(QStringLiteral("http://google.com/complete/search?output=toolbar&q=%1")); //! [1] //! [2] @@ -77,11 +77,10 @@ GSuggestCompletion::GSuggestCompletion(QLineEdit *parent): QObject(parent), edit connect(popup, SIGNAL(itemClicked(QTreeWidgetItem*,int)), SLOT(doneCompletion())); - timer = new QTimer(this); - timer->setSingleShot(true); - timer->setInterval(500); - connect(timer, SIGNAL(timeout()), SLOT(autoSuggest())); - connect(editor, SIGNAL(textEdited(QString)), timer, SLOT(start())); + timer.setSingleShot(true); + timer.setInterval(500); + connect(&timer, SIGNAL(timeout()), SLOT(autoSuggest())); + connect(editor, SIGNAL(textEdited(QString)), &timer, SLOT(start())); connect(&networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(handleNetworkData(QNetworkReply*))); @@ -109,7 +108,6 @@ bool GSuggestCompletion::eventFilter(QObject *obj, QEvent *ev) } if (ev->type() == QEvent::KeyPress) { - bool consumed = false; int key = static_cast<QKeyEvent*>(ev)->key(); switch (key) { @@ -148,9 +146,8 @@ bool GSuggestCompletion::eventFilter(QObject *obj, QEvent *ev) //! [4] //! [5] -void GSuggestCompletion::showCompletion(const QStringList &choices) +void GSuggestCompletion::showCompletion(const QVector<QString> &choices) { - if (choices.isEmpty()) return; @@ -159,12 +156,13 @@ void GSuggestCompletion::showCompletion(const QStringList &choices) popup->setUpdatesEnabled(false); popup->clear(); - for (int i = 0; i < choices.count(); ++i) { - QTreeWidgetItem * item; - item = new QTreeWidgetItem(popup); - item->setText(0, choices[i]); + + for (const auto &choice : choices) { + auto item = new QTreeWidgetItem(popup); + item->setText(0, choice); item->setTextColor(0, color); } + popup->setCurrentItem(popup->topLevelItem(0)); popup->resizeColumnToContents(0); popup->setUpdatesEnabled(true); @@ -178,7 +176,7 @@ void GSuggestCompletion::showCompletion(const QStringList &choices) //! [6] void GSuggestCompletion::doneCompletion() { - timer->stop(); + timer.stop(); popup->hide(); editor->setFocus(); QTreeWidgetItem *item = popup->currentItem(); @@ -193,15 +191,15 @@ void GSuggestCompletion::doneCompletion() void GSuggestCompletion::autoSuggest() { QString str = editor->text(); - QString url = QString(GSUGGEST_URL).arg(str); - networkManager.get(QNetworkRequest(QString(url))); + QString url = gsuggestUrl.arg(str); + networkManager.get(QNetworkRequest(url)); } //! [7] //! [8] void GSuggestCompletion::preventSuggest() { - timer->stop(); + timer.stop(); } //! [8] @@ -209,8 +207,8 @@ void GSuggestCompletion::preventSuggest() void GSuggestCompletion::handleNetworkData(QNetworkReply *networkReply) { QUrl url = networkReply->url(); - if (!networkReply->error()) { - QStringList choices; + if (networkReply->error() == QNetworkReply::NoError) { + QVector<QString> choices; QByteArray response(networkReply->readAll()); QXmlStreamReader xml(response); diff --git a/examples/network/googlesuggest/googlesuggest.h b/examples/network/googlesuggest/googlesuggest.h index 1d42f31571..a0b0ac069c 100644 --- a/examples/network/googlesuggest/googlesuggest.h +++ b/examples/network/googlesuggest/googlesuggest.h @@ -53,14 +53,7 @@ #include <QtWidgets> #include <QtNetwork> -#include <QObject> - -QT_BEGIN_NAMESPACE -class QLineEdit; -class QNetworkReply; -class QTimer; -class QTreeWidget; -QT_END_NAMESPACE +#include <QtCore> //! [1] class GSuggestCompletion : public QObject @@ -68,10 +61,10 @@ class GSuggestCompletion : public QObject Q_OBJECT public: - GSuggestCompletion(QLineEdit *parent = 0); + explicit GSuggestCompletion(QLineEdit *parent = nullptr); ~GSuggestCompletion(); bool eventFilter(QObject *obj, QEvent *ev) override; - void showCompletion(const QStringList &choices); + void showCompletion(const QVector<QString> &choices); public slots: @@ -81,9 +74,9 @@ public slots: void handleNetworkData(QNetworkReply *networkReply); private: - QLineEdit *editor; - QTreeWidget *popup; - QTimer *timer; + QLineEdit *editor = nullptr; + QTreeWidget *popup = nullptr; + QTimer timer; QNetworkAccessManager networkManager; }; //! [1] diff --git a/examples/network/googlesuggest/main.cpp b/examples/network/googlesuggest/main.cpp index 9de966d7a5..ab819c5502 100644 --- a/examples/network/googlesuggest/main.cpp +++ b/examples/network/googlesuggest/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. @@ -55,7 +55,7 @@ int main(int argc, char * argv[]) { QApplication app(argc, argv); - SearchBox *searchEdit = new SearchBox; - searchEdit->show(); + SearchBox searchEdit; + searchEdit.show(); return app.exec(); } diff --git a/examples/network/googlesuggest/searchbox.cpp b/examples/network/googlesuggest/searchbox.cpp index e0754a7de2..d0bdb70daa 100644 --- a/examples/network/googlesuggest/searchbox.cpp +++ b/examples/network/googlesuggest/searchbox.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. @@ -54,7 +54,7 @@ #include "searchbox.h" #include "googlesuggest.h" -#define GSEARCH_URL "http://www.google.com/search?q=%1" +const QString gsearchUrl = QStringLiteral("http://www.google.com/search?q=%1"); //! [1] SearchBox::SearchBox(QWidget *parent): QLineEdit(parent) @@ -75,8 +75,8 @@ SearchBox::SearchBox(QWidget *parent): QLineEdit(parent) void SearchBox::doSearch() { completer->preventSuggest(); - QString url = QString(GSEARCH_URL).arg(text()); - QDesktopServices::openUrl(QUrl(url)); + QString url = gsearchUrl.arg(text()); + QDesktopServices::openUrl(url); } //! [2] diff --git a/examples/network/googlesuggest/searchbox.h b/examples/network/googlesuggest/searchbox.h index eea5854de8..fbd33011b7 100644 --- a/examples/network/googlesuggest/searchbox.h +++ b/examples/network/googlesuggest/searchbox.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. @@ -61,13 +61,13 @@ class SearchBox: public QLineEdit Q_OBJECT public: - SearchBox(QWidget *parent = 0); + explicit SearchBox(QWidget *parent = nullptr); protected slots: void doSearch(); private: - GSuggestCompletion *completer; + GSuggestCompletion *completer = nullptr; //! [1] }; diff --git a/examples/network/multicastreceiver/receiver.cpp b/examples/network/multicastreceiver/receiver.cpp index 10154c60cc..8985ad1d82 100644 --- a/examples/network/multicastreceiver/receiver.cpp +++ b/examples/network/multicastreceiver/receiver.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. @@ -54,41 +54,39 @@ #include "receiver.h" Receiver::Receiver(QWidget *parent) - : QDialog(parent) + : QDialog(parent), + groupAddress(QStringLiteral("239.255.43.21")) { - groupAddress = QHostAddress("239.255.43.21"); - statusLabel = new QLabel(tr("Listening for multicasted messages")); - quitButton = new QPushButton(tr("&Quit")); - - udpSocket = new QUdpSocket(this); - udpSocket->bind(QHostAddress::AnyIPv4, 45454, QUdpSocket::ShareAddress); - udpSocket->joinMulticastGroup(groupAddress); - - connect(udpSocket, SIGNAL(readyRead()), - this, SLOT(processPendingDatagrams())); - connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); + auto quitButton = new QPushButton(tr("&Quit")); - QHBoxLayout *buttonLayout = new QHBoxLayout; + auto buttonLayout = new QHBoxLayout; buttonLayout->addStretch(1); buttonLayout->addWidget(quitButton); buttonLayout->addStretch(1); - QVBoxLayout *mainLayout = new QVBoxLayout; + auto mainLayout = new QVBoxLayout; mainLayout->addWidget(statusLabel); mainLayout->addLayout(buttonLayout); setLayout(mainLayout); setWindowTitle(tr("Multicast Receiver")); + + udpSocket.bind(QHostAddress::AnyIPv4, 45454, QUdpSocket::ShareAddress); + udpSocket.joinMulticastGroup(groupAddress); + + connect(&udpSocket, SIGNAL(readyRead()), + this, SLOT(processPendingDatagrams())); + connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); } void Receiver::processPendingDatagrams() { - while (udpSocket->hasPendingDatagrams()) { - QByteArray datagram; - datagram.resize(udpSocket->pendingDatagramSize()); - udpSocket->readDatagram(datagram.data(), datagram.size()); + QByteArray datagram; + while (udpSocket.hasPendingDatagrams()) { + datagram.resize(int(udpSocket.pendingDatagramSize())); + udpSocket.readDatagram(datagram.data(), datagram.size()); statusLabel->setText(tr("Received datagram: \"%1\"") - .arg(datagram.data())); + .arg(datagram.constData())); } } diff --git a/examples/network/multicastreceiver/receiver.h b/examples/network/multicastreceiver/receiver.h index efef1cdb30..54927fdd63 100644 --- a/examples/network/multicastreceiver/receiver.h +++ b/examples/network/multicastreceiver/receiver.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. @@ -53,11 +53,10 @@ #include <QDialog> #include <QHostAddress> +#include <QUdpSocket> QT_BEGIN_NAMESPACE class QLabel; -class QPushButton; -class QUdpSocket; QT_END_NAMESPACE class Receiver : public QDialog @@ -65,15 +64,14 @@ class Receiver : public QDialog Q_OBJECT public: - Receiver(QWidget *parent = 0); + explicit Receiver(QWidget *parent = nullptr); private slots: void processPendingDatagrams(); private: - QLabel *statusLabel; - QPushButton *quitButton; - QUdpSocket *udpSocket; + QLabel *statusLabel = nullptr; + QUdpSocket udpSocket; QHostAddress groupAddress; }; |