summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorMorten Engvoldsen <morten.engvoldsen@nokia.com>2009-11-09 12:02:51 (GMT)
committerMorten Engvoldsen <morten.engvoldsen@nokia.com>2009-11-09 12:02:51 (GMT)
commita7577f8c3ae3a025384d8d19fdf3575ef141a70b (patch)
tree9c2f7c74f7aabedcef46e435aa8ada3f3708fb10 /examples
parentd9c5a6828e0f5c846aeef214f2fe0a9de48072dc (diff)
downloadQt-a7577f8c3ae3a025384d8d19fdf3575ef141a70b.zip
Qt-a7577f8c3ae3a025384d8d19fdf3575ef141a70b.tar.gz
Qt-a7577f8c3ae3a025384d8d19fdf3575ef141a70b.tar.bz2
˙ūd
Diffstat (limited to 'examples')
-rw-r--r--examples/network/googlesuggest/googlesuggest.cpp58
-rw-r--r--examples/network/googlesuggest/googlesuggest.h6
-rw-r--r--examples/network/googlesuggest/searchbox.cpp7
-rw-r--r--examples/network/googlesuggest/searchbox.h2
4 files changed, 46 insertions, 27 deletions
diff --git a/examples/network/googlesuggest/googlesuggest.cpp b/examples/network/googlesuggest/googlesuggest.cpp
index e1588a6..a1075ec 100644
--- a/examples/network/googlesuggest/googlesuggest.cpp
+++ b/examples/network/googlesuggest/googlesuggest.cpp
@@ -39,17 +39,22 @@
**
****************************************************************************/
-#include <QtCore>
-#include <QtGui>
-#include <QtNetwork>
+//! [1]
#include "googlesuggest.h"
#define GSUGGEST_URL "http://google.com/complete/search?output=toolbar&q=%1"
+//! [1]
+//! [2]
GSuggestCompletion::GSuggestCompletion(QLineEdit *parent): QObject(parent), editor(parent)
{
popup = new QTreeWidget;
+ popup->setWindowFlags(Qt::Popup);
+ popup->setFocusPolicy(Qt::NoFocus);
+ popup->setFocusProxy(parent);
+ popup->setMouseTracking(true);
+
popup->setColumnCount(2);
popup->setUniformRowHeights(true);
popup->setRootIsDecorated(false);
@@ -57,18 +62,13 @@ GSuggestCompletion::GSuggestCompletion(QLineEdit *parent): QObject(parent), edit
popup->setSelectionBehavior(QTreeWidget::SelectRows);
popup->setFrameStyle(QFrame::Box | QFrame::Plain);
popup->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
-
popup->header()->hide();
+
popup->installEventFilter(this);
- popup->setMouseTracking(true);
connect(popup, SIGNAL(itemClicked(QTreeWidgetItem*, int)),
SLOT(doneCompletion()));
- popup->setWindowFlags(Qt::Popup);
- popup->setFocusPolicy(Qt::NoFocus);
- popup->setFocusProxy(parent);
-
timer = new QTimer(this);
timer->setSingleShot(true);
timer->setInterval(500);
@@ -79,12 +79,16 @@ GSuggestCompletion::GSuggestCompletion(QLineEdit *parent): QObject(parent), edit
this, SLOT(handleNetworkData(QNetworkReply*)));
}
+//! [2]
+//! [3]
GSuggestCompletion::~GSuggestCompletion()
{
delete popup;
}
+//! [3]
+//! [4]
bool GSuggestCompletion::eventFilter(QObject *obj, QEvent *ev)
{
if (obj != popup)
@@ -131,9 +135,12 @@ bool GSuggestCompletion::eventFilter(QObject *obj, QEvent *ev)
return false;
}
+//! [4]
+//! [5]
void GSuggestCompletion::showCompletion(const QStringList &choices, const QStringList &hits)
{
+
if (choices.isEmpty() || choices.count() != hits.count())
return;
@@ -163,7 +170,9 @@ void GSuggestCompletion::showCompletion(const QStringList &choices, const QStrin
popup->setFocus();
popup->show();
}
+//! [5]
+//! [6]
void GSuggestCompletion::doneCompletion()
{
timer->stop();
@@ -172,26 +181,28 @@ void GSuggestCompletion::doneCompletion()
QTreeWidgetItem *item = popup->currentItem();
if (item) {
editor->setText(item->text(0));
- QKeyEvent *e;
- e = new QKeyEvent(QEvent::KeyPress, Qt::Key_Enter, Qt::NoModifier);
- QApplication::postEvent(editor, e);
- e = new QKeyEvent(QEvent::KeyRelease, Qt::Key_Enter, Qt::NoModifier);
- QApplication::postEvent(editor, e);
+ QMetaObject::invokeMethod(editor, "returnPressed");
}
}
+//! [6]
-void GSuggestCompletion::preventSuggest()
-{
- timer->stop();
-}
-
+//! [7]
void GSuggestCompletion::autoSuggest()
{
QString str = editor->text();
QString url = QString(GSUGGEST_URL).arg(str);
networkManager.get(QNetworkRequest(QString(url)));
}
+//! [7]
+
+//! [8]
+void GSuggestCompletion::preventSuggest()
+{
+ timer->stop();
+}
+//! [8]
+//! [9]
void GSuggestCompletion::handleNetworkData(QNetworkReply *networkReply)
{
QUrl url = networkReply->url();
@@ -199,20 +210,20 @@ void GSuggestCompletion::handleNetworkData(QNetworkReply *networkReply)
QStringList choices;
QStringList hits;
- QString response(networkReply->readAll());
+ QByteArray response(networkReply->readAll());
QXmlStreamReader xml(response);
while (!xml.atEnd()) {
xml.readNext();
- if (xml.isStartElement()) {
+ if (xml.tokenType() == QXmlStreamReader::StartElement)
if (xml.name() == "suggestion") {
QStringRef str = xml.attributes().value("data");
choices << str.toString();
}
- else if (xml.name() == "num_queries") {
+ if (xml.tokenType() == QXmlStreamReader::StartElement)
+ if (xml.name() == "num_queries") {
QStringRef str = xml.attributes().value("int");
hits << str.toString();
}
- }
}
showCompletion(choices, hits);
@@ -220,3 +231,4 @@ void GSuggestCompletion::handleNetworkData(QNetworkReply *networkReply)
networkReply->deleteLater();
}
+//! [9] \ No newline at end of file
diff --git a/examples/network/googlesuggest/googlesuggest.h b/examples/network/googlesuggest/googlesuggest.h
index 2a3c878..c33df36 100644
--- a/examples/network/googlesuggest/googlesuggest.h
+++ b/examples/network/googlesuggest/googlesuggest.h
@@ -42,8 +42,9 @@
#ifndef GOOGLESUGGEST_H
#define GOOGLESUGGEST_H
+#include <QtGui>
+#include <QtNetwork>
#include <QObject>
-#include <QNetworkAccessManager>
QT_BEGIN_NAMESPACE
class QLineEdit;
@@ -52,6 +53,7 @@ class QTimer;
class QTreeWidget;
QT_END_NAMESPACE
+//! [1]
class GSuggestCompletion : public QObject
{
Q_OBJECT
@@ -75,6 +77,6 @@ private:
QTimer *timer;
QNetworkAccessManager networkManager;
};
-
+//! [1]
#endif // GOOGLESUGGEST_H
diff --git a/examples/network/googlesuggest/searchbox.cpp b/examples/network/googlesuggest/searchbox.cpp
index 21599e0..ae08a75 100644
--- a/examples/network/googlesuggest/searchbox.cpp
+++ b/examples/network/googlesuggest/searchbox.cpp
@@ -47,12 +47,12 @@
#define GSEARCH_URL "http://www.google.com/search?q=%1"
-
+//! [1]
SearchBox::SearchBox(QWidget *parent): QLineEdit(parent)
{
completer = new GSuggestCompletion(this);
- connect(this, SIGNAL(returnPressed()), SLOT(doSearch()));
+ connect(this, SIGNAL(returnPressed()),this, SLOT(doSearch()));
setWindowTitle("Search with Google");
@@ -60,10 +60,13 @@ SearchBox::SearchBox(QWidget *parent): QLineEdit(parent)
resize(400, height());
setFocus();
}
+//! [1]
+//! [2]
void SearchBox::doSearch()
{
completer->preventSuggest();
QString url = QString(GSEARCH_URL).arg(text());
QDesktopServices::openUrl(QUrl(url));
}
+//! [2] \ No newline at end of file
diff --git a/examples/network/googlesuggest/searchbox.h b/examples/network/googlesuggest/searchbox.h
index 4b03dba..ec18bb0 100644
--- a/examples/network/googlesuggest/searchbox.h
+++ b/examples/network/googlesuggest/searchbox.h
@@ -42,6 +42,7 @@
#ifndef SEARCHBOX_H
#define SEARCHBOX_H
+//! [1]
#include <QLineEdit>
class GSuggestCompletion;
@@ -58,6 +59,7 @@ protected slots:
private:
GSuggestCompletion *completer;
+//! [1]
};