This repository has been archived by the owner on Sep 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathgooglesuggest.qdoc
101 lines (77 loc) · 3.7 KB
/
googlesuggest.qdoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/****************************************************************************
**
** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** GNU Free Documentation License
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms
** and conditions contained in a signed written agreement between you
** and Nokia.
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
/*!
\page googlesuggest
\example googlesuggest
\title Google Suggest Example
\section1 Description
The Google Suggest example demonstrates how to use the QNetworkAccessManager
class to obtain a list of suggestions from the Google search engine as the
user types into a TextField.
\image googlesuggest-example.png
The application makes use of the \c get function in
QNetworkAccessManager to post a request and obtain the result of the search
query sent to the Google search engine. The results returned are put into
a ListView.
This example is built up by a TextField as the search box, and a ListView
used to store the suggestions.
\section1 GoogleSuggest Class Declaration
This class implements slots for various signals and a number of functions to display
the search results and to determent when and how to perform the search.
\snippet googlesuggest/src/googlesuggest.hpp 1
The class connects to a TextField and uses a ListView to display the
results. Both are defined in the QML. A QTimer controls the start of the
network requests that are executed using a QNetworkAccessManager.
\snippet googlesuggest/assets/main.qml 1
\section1 GoogleSuggest Class Implementation
We start by defining a constant c-string containing the URL to be used in the Google
queries. This is the basis for the query. The letters typed into the search
box will be added to the query to perform the search itself.
\snippet googlesuggest/src/googlesuggest.cpp 1
A single-shot QTimer is used to start the request when the user has stopped
typing for 500 ms.
Finally, we connect the networkManagers \c finished() signal with the \c
handleNetworkData() slot to handle the incoming data.
\snippet googlesuggest/src/googlesuggest.cpp 2
The \c showCompletion() function updates the data model connected to our
ListView. It takes two QStringLists, one with the suggested
search terms and the other with the corresponding number of hits.
\snippet googlesuggest/src/googlesuggest.cpp 3
The \c autoSuggest() slot is called when the timer times out, and uses the
text in the TextField to build the complete search query. The query is then
passed to the QNetworkAccessManager's \c get() function to start the
request.
\snippet googlesuggest/src/googlesuggest.cpp 4
To extract the data from the reply we use the \c readAll() function, which
is inherited from QIODevice and returns a QByteArray. Since this data is
encoded in XML we can use a QXmlStreamReader to traverse the data and
extract the search result as QStrings, which we can stream into two
QStringLists used to populate the popup.
Finally, we schedule the QNetworkReply object for deletion using the \c
deleteLater function.
\snippet googlesuggest/src/googlesuggest.cpp 5
*/