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>
This commit is contained in:
Timur Pocheptsov 2017-09-29 13:57:43 +02:00
parent 5b4cf7af6a
commit aeee3be166
5 changed files with 33 additions and 42 deletions

View File

@ -51,7 +51,7 @@
//! [1] //! [1]
#include "googlesuggest.h" #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] //! [1]
//! [2] //! [2]
@ -77,11 +77,10 @@ GSuggestCompletion::GSuggestCompletion(QLineEdit *parent): QObject(parent), edit
connect(popup, SIGNAL(itemClicked(QTreeWidgetItem*,int)), connect(popup, SIGNAL(itemClicked(QTreeWidgetItem*,int)),
SLOT(doneCompletion())); SLOT(doneCompletion()));
timer = new QTimer(this); timer.setSingleShot(true);
timer->setSingleShot(true); timer.setInterval(500);
timer->setInterval(500); connect(&timer, SIGNAL(timeout()), SLOT(autoSuggest()));
connect(timer, SIGNAL(timeout()), SLOT(autoSuggest())); connect(editor, SIGNAL(textEdited(QString)), &timer, SLOT(start()));
connect(editor, SIGNAL(textEdited(QString)), timer, SLOT(start()));
connect(&networkManager, SIGNAL(finished(QNetworkReply*)), connect(&networkManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(handleNetworkData(QNetworkReply*))); this, SLOT(handleNetworkData(QNetworkReply*)));
@ -109,7 +108,6 @@ bool GSuggestCompletion::eventFilter(QObject *obj, QEvent *ev)
} }
if (ev->type() == QEvent::KeyPress) { if (ev->type() == QEvent::KeyPress) {
bool consumed = false; bool consumed = false;
int key = static_cast<QKeyEvent*>(ev)->key(); int key = static_cast<QKeyEvent*>(ev)->key();
switch (key) { switch (key) {
@ -148,9 +146,8 @@ bool GSuggestCompletion::eventFilter(QObject *obj, QEvent *ev)
//! [4] //! [4]
//! [5] //! [5]
void GSuggestCompletion::showCompletion(const QStringList &choices) void GSuggestCompletion::showCompletion(const QVector<QString> &choices)
{ {
if (choices.isEmpty()) if (choices.isEmpty())
return; return;
@ -159,12 +156,13 @@ void GSuggestCompletion::showCompletion(const QStringList &choices)
popup->setUpdatesEnabled(false); popup->setUpdatesEnabled(false);
popup->clear(); popup->clear();
for (int i = 0; i < choices.count(); ++i) {
QTreeWidgetItem * item; for (const auto &choice : choices) {
item = new QTreeWidgetItem(popup); auto item = new QTreeWidgetItem(popup);
item->setText(0, choices[i]); item->setText(0, choice);
item->setTextColor(0, color); item->setTextColor(0, color);
} }
popup->setCurrentItem(popup->topLevelItem(0)); popup->setCurrentItem(popup->topLevelItem(0));
popup->resizeColumnToContents(0); popup->resizeColumnToContents(0);
popup->setUpdatesEnabled(true); popup->setUpdatesEnabled(true);
@ -178,7 +176,7 @@ void GSuggestCompletion::showCompletion(const QStringList &choices)
//! [6] //! [6]
void GSuggestCompletion::doneCompletion() void GSuggestCompletion::doneCompletion()
{ {
timer->stop(); timer.stop();
popup->hide(); popup->hide();
editor->setFocus(); editor->setFocus();
QTreeWidgetItem *item = popup->currentItem(); QTreeWidgetItem *item = popup->currentItem();
@ -193,15 +191,15 @@ void GSuggestCompletion::doneCompletion()
void GSuggestCompletion::autoSuggest() void GSuggestCompletion::autoSuggest()
{ {
QString str = editor->text(); QString str = editor->text();
QString url = QString(GSUGGEST_URL).arg(str); QString url = gsuggestUrl.arg(str);
networkManager.get(QNetworkRequest(QString(url))); networkManager.get(QNetworkRequest(url));
} }
//! [7] //! [7]
//! [8] //! [8]
void GSuggestCompletion::preventSuggest() void GSuggestCompletion::preventSuggest()
{ {
timer->stop(); timer.stop();
} }
//! [8] //! [8]
@ -209,8 +207,8 @@ void GSuggestCompletion::preventSuggest()
void GSuggestCompletion::handleNetworkData(QNetworkReply *networkReply) void GSuggestCompletion::handleNetworkData(QNetworkReply *networkReply)
{ {
QUrl url = networkReply->url(); QUrl url = networkReply->url();
if (!networkReply->error()) { if (networkReply->error() == QNetworkReply::NoError) {
QStringList choices; QVector<QString> choices;
QByteArray response(networkReply->readAll()); QByteArray response(networkReply->readAll());
QXmlStreamReader xml(response); QXmlStreamReader xml(response);

View File

@ -53,14 +53,7 @@
#include <QtWidgets> #include <QtWidgets>
#include <QtNetwork> #include <QtNetwork>
#include <QObject> #include <QtCore>
QT_BEGIN_NAMESPACE
class QLineEdit;
class QNetworkReply;
class QTimer;
class QTreeWidget;
QT_END_NAMESPACE
//! [1] //! [1]
class GSuggestCompletion : public QObject class GSuggestCompletion : public QObject
@ -68,10 +61,10 @@ class GSuggestCompletion : public QObject
Q_OBJECT Q_OBJECT
public: public:
GSuggestCompletion(QLineEdit *parent = 0); explicit GSuggestCompletion(QLineEdit *parent = nullptr);
~GSuggestCompletion(); ~GSuggestCompletion();
bool eventFilter(QObject *obj, QEvent *ev) override; bool eventFilter(QObject *obj, QEvent *ev) override;
void showCompletion(const QStringList &choices); void showCompletion(const QVector<QString> &choices);
public slots: public slots:
@ -81,9 +74,9 @@ public slots:
void handleNetworkData(QNetworkReply *networkReply); void handleNetworkData(QNetworkReply *networkReply);
private: private:
QLineEdit *editor; QLineEdit *editor = nullptr;
QTreeWidget *popup; QTreeWidget *popup = nullptr;
QTimer *timer; QTimer timer;
QNetworkAccessManager networkManager; QNetworkAccessManager networkManager;
}; };
//! [1] //! [1]

View File

@ -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.
@ -55,7 +55,7 @@
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
QApplication app(argc, argv); QApplication app(argc, argv);
SearchBox *searchEdit = new SearchBox; SearchBox searchEdit;
searchEdit->show(); searchEdit.show();
return app.exec(); return app.exec();
} }

View File

@ -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.
@ -54,7 +54,7 @@
#include "searchbox.h" #include "searchbox.h"
#include "googlesuggest.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] //! [1]
SearchBox::SearchBox(QWidget *parent): QLineEdit(parent) SearchBox::SearchBox(QWidget *parent): QLineEdit(parent)
@ -75,8 +75,8 @@ SearchBox::SearchBox(QWidget *parent): QLineEdit(parent)
void SearchBox::doSearch() void SearchBox::doSearch()
{ {
completer->preventSuggest(); completer->preventSuggest();
QString url = QString(GSEARCH_URL).arg(text()); QString url = gsearchUrl.arg(text());
QDesktopServices::openUrl(QUrl(url)); QDesktopServices::openUrl(url);
} }
//! [2] //! [2]

View File

@ -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.
@ -61,13 +61,13 @@ class SearchBox: public QLineEdit
Q_OBJECT Q_OBJECT
public: public:
SearchBox(QWidget *parent = 0); explicit SearchBox(QWidget *parent = nullptr);
protected slots: protected slots:
void doSearch(); void doSearch();
private: private:
GSuggestCompletion *completer; GSuggestCompletion *completer = nullptr;
//! [1] //! [1]
}; };