summaryrefslogtreecommitdiffstats
path: root/examples/network/googlesuggest/googlesuggest.cpp
diff options
context:
space:
mode:
authorTimur Pocheptsov <timur.pocheptsov@qt.io>2017-09-29 13:57:43 +0200
committerTimur Pocheptsov <timur.pocheptsov@qt.io>2017-10-05 03:45:26 +0000
commitaeee3be166aff737990fe2cade05bff7b39dfebc (patch)
treed1e5a2085bd0df3cd204a4b0121e5178f1a61862 /examples/network/googlesuggest/googlesuggest.cpp
parent5b4cf7af6a492b6ef5b7c718b346b38ddad4c990 (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/network/googlesuggest/googlesuggest.cpp')
-rw-r--r--examples/network/googlesuggest/googlesuggest.cpp36
1 files changed, 17 insertions, 19 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);