Example: Removed the code to handle 'num_entries' info.
The Google Suggest API doesn't return the 'num_queries' anymore. Had to remove code related to 'num_queries' entry so that the suggestion list shows up. Task-number: QTBUG-42817 Change-Id: Ic918d1c86840fa4c1e18f32a984f5a9dd911261d Reviewed-by: Topi Reiniö <topi.reinio@digia.com>
This commit is contained in:
parent
b0ae0db61a
commit
3e6a14168b
Binary file not shown.
Before Width: | Height: | Size: 8.8 KiB After Width: | Height: | Size: 21 KiB |
@ -68,12 +68,8 @@
|
|||||||
in the explicit \c editor member variable.
|
in the explicit \c editor member variable.
|
||||||
|
|
||||||
We then create a QTreeWidget as a toplevel widget and configure the various
|
We then create a QTreeWidget as a toplevel widget and configure the various
|
||||||
properties to give it the look of a popup widget.
|
properties to give it the look of a popup widget. The widget is populated
|
||||||
|
with the results by Google Suggest API request.
|
||||||
The popup will be populated by the results returned from Google. We set
|
|
||||||
the number of columns to be two, since we want to display both the
|
|
||||||
suggested search term and the number of hits it will trigger in the search
|
|
||||||
engine.
|
|
||||||
|
|
||||||
Furthermore, we install the GSuggestCompletion instance as an event filter
|
Furthermore, we install the GSuggestCompletion instance as an event filter
|
||||||
on the QTreeWidget, and connect the \c itemClicked() signal with the \c
|
on the QTreeWidget, and connect the \c itemClicked() signal with the \c
|
||||||
@ -110,8 +106,8 @@
|
|||||||
\snippet googlesuggest/googlesuggest.cpp 4
|
\snippet googlesuggest/googlesuggest.cpp 4
|
||||||
|
|
||||||
The \c showCompletion() function populates the QTreeWidget with the results
|
The \c showCompletion() function populates the QTreeWidget with the results
|
||||||
returned from the query. It takes two QStringLists, one with the suggested
|
returned from the query. It takes a QStringList of the suggested search
|
||||||
search terms and the other with the corresponding number of hits.
|
terms.
|
||||||
|
|
||||||
\snippet googlesuggest/googlesuggest.cpp 5
|
\snippet googlesuggest/googlesuggest.cpp 5
|
||||||
|
|
||||||
|
@ -38,7 +38,6 @@
|
|||||||
**
|
**
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
//! [1]
|
//! [1]
|
||||||
#include "googlesuggest.h"
|
#include "googlesuggest.h"
|
||||||
|
|
||||||
@ -54,7 +53,7 @@ GSuggestCompletion::GSuggestCompletion(QLineEdit *parent): QObject(parent), edit
|
|||||||
popup->setFocusProxy(parent);
|
popup->setFocusProxy(parent);
|
||||||
popup->setMouseTracking(true);
|
popup->setMouseTracking(true);
|
||||||
|
|
||||||
popup->setColumnCount(2);
|
popup->setColumnCount(1);
|
||||||
popup->setUniformRowHeights(true);
|
popup->setUniformRowHeights(true);
|
||||||
popup->setRootIsDecorated(false);
|
popup->setRootIsDecorated(false);
|
||||||
popup->setEditTriggers(QTreeWidget::NoEditTriggers);
|
popup->setEditTriggers(QTreeWidget::NoEditTriggers);
|
||||||
@ -137,10 +136,10 @@ bool GSuggestCompletion::eventFilter(QObject *obj, QEvent *ev)
|
|||||||
//! [4]
|
//! [4]
|
||||||
|
|
||||||
//! [5]
|
//! [5]
|
||||||
void GSuggestCompletion::showCompletion(const QStringList &choices, const QStringList &hits)
|
void GSuggestCompletion::showCompletion(const QStringList &choices)
|
||||||
{
|
{
|
||||||
|
|
||||||
if (choices.isEmpty() || choices.count() != hits.count())
|
if (choices.isEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const QPalette &pal = editor->palette();
|
const QPalette &pal = editor->palette();
|
||||||
@ -152,19 +151,12 @@ void GSuggestCompletion::showCompletion(const QStringList &choices, const QStrin
|
|||||||
QTreeWidgetItem * item;
|
QTreeWidgetItem * item;
|
||||||
item = new QTreeWidgetItem(popup);
|
item = new QTreeWidgetItem(popup);
|
||||||
item->setText(0, choices[i]);
|
item->setText(0, choices[i]);
|
||||||
item->setText(1, hits[i]);
|
item->setTextColor(0, color);
|
||||||
item->setTextAlignment(1, Qt::AlignRight);
|
|
||||||
item->setTextColor(1, color);
|
|
||||||
}
|
}
|
||||||
popup->setCurrentItem(popup->topLevelItem(0));
|
popup->setCurrentItem(popup->topLevelItem(0));
|
||||||
popup->resizeColumnToContents(0);
|
popup->resizeColumnToContents(0);
|
||||||
popup->resizeColumnToContents(1);
|
|
||||||
popup->adjustSize();
|
|
||||||
popup->setUpdatesEnabled(true);
|
popup->setUpdatesEnabled(true);
|
||||||
|
|
||||||
int h = popup->sizeHintForRow(0) * qMin(7, choices.count()) + 3;
|
|
||||||
popup->resize(popup->width(), h);
|
|
||||||
|
|
||||||
popup->move(editor->mapToGlobal(QPoint(0, editor->height())));
|
popup->move(editor->mapToGlobal(QPoint(0, editor->height())));
|
||||||
popup->setFocus();
|
popup->setFocus();
|
||||||
popup->show();
|
popup->show();
|
||||||
@ -207,7 +199,6 @@ void GSuggestCompletion::handleNetworkData(QNetworkReply *networkReply)
|
|||||||
QUrl url = networkReply->url();
|
QUrl url = networkReply->url();
|
||||||
if (!networkReply->error()) {
|
if (!networkReply->error()) {
|
||||||
QStringList choices;
|
QStringList choices;
|
||||||
QStringList hits;
|
|
||||||
|
|
||||||
QByteArray response(networkReply->readAll());
|
QByteArray response(networkReply->readAll());
|
||||||
QXmlStreamReader xml(response);
|
QXmlStreamReader xml(response);
|
||||||
@ -218,17 +209,11 @@ void GSuggestCompletion::handleNetworkData(QNetworkReply *networkReply)
|
|||||||
QStringRef str = xml.attributes().value("data");
|
QStringRef str = xml.attributes().value("data");
|
||||||
choices << str.toString();
|
choices << str.toString();
|
||||||
}
|
}
|
||||||
if (xml.tokenType() == QXmlStreamReader::StartElement)
|
|
||||||
if (xml.name() == "num_queries") {
|
|
||||||
QStringRef str = xml.attributes().value("int");
|
|
||||||
hits << str.toString();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
showCompletion(choices, hits);
|
showCompletion(choices);
|
||||||
}
|
}
|
||||||
|
|
||||||
networkReply->deleteLater();
|
networkReply->deleteLater();
|
||||||
}
|
}
|
||||||
//! [9]
|
//! [9]
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ public:
|
|||||||
GSuggestCompletion(QLineEdit *parent = 0);
|
GSuggestCompletion(QLineEdit *parent = 0);
|
||||||
~GSuggestCompletion();
|
~GSuggestCompletion();
|
||||||
bool eventFilter(QObject *obj, QEvent *ev) Q_DECL_OVERRIDE;
|
bool eventFilter(QObject *obj, QEvent *ev) Q_DECL_OVERRIDE;
|
||||||
void showCompletion(const QStringList &choices, const QStringList &hits);
|
void showCompletion(const QStringList &choices);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user