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:
parent
5b4cf7af6a
commit
aeee3be166
@ -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);
|
||||
|
@ -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]
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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]
|
||||
|
||||
|
@ -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]
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user