diff options
author | Timur Pocheptsov <timur.pocheptsov@qt.io> | 2017-09-29 13:57:43 +0200 |
---|---|---|
committer | Timur Pocheptsov <timur.pocheptsov@qt.io> | 2017-10-05 03:45:26 +0000 |
commit | aeee3be166aff737990fe2cade05bff7b39dfebc (patch) | |
tree | d1e5a2085bd0df3cd204a4b0121e5178f1a61862 /examples | |
parent | 5b4cf7af6a492b6ef5b7c718b346b38ddad4c990 (diff) |
QtNetwork (examples) - update googlesuggest example
Mainly cosmetic - nullptr, explicit, QVector<QString> etc.
Plus: do not leak SearchBox.
Task-number: QTBUG-60628
Change-Id: I4c538ced64a469fbe4627f44d2d883e6dcd2362e
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'examples')
-rw-r--r-- | examples/network/googlesuggest/googlesuggest.cpp | 36 | ||||
-rw-r--r-- | examples/network/googlesuggest/googlesuggest.h | 19 | ||||
-rw-r--r-- | examples/network/googlesuggest/main.cpp | 6 | ||||
-rw-r--r-- | examples/network/googlesuggest/searchbox.cpp | 8 | ||||
-rw-r--r-- | examples/network/googlesuggest/searchbox.h | 6 |
5 files changed, 33 insertions, 42 deletions
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] }; |