diff options
author | Milian Wolff <milian.wolff@kdab.com> | 2013-12-19 14:26:02 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-12-20 13:00:22 +0100 |
commit | 1701720dc6c2df030ee5f7db4d8aa6e2422654db (patch) | |
tree | a03ca645d0b98e6c7784de965b3dfc2720dcd35d | |
parent | fb587a3a996d2e9d2fca34cc053df29f6d23f64e (diff) | |
download | qtwebchannel-1701720dc6c2df030ee5f7db4d8aa6e2422654db.tar.gz |
Add standalone C++/Qt example which opens HTML client in a browser.
This example shows how to use the (currently quite ugly) raw C++ API
to setup a webchannel without using QML at all. The HTML client is then
handled by the users default browser.
The example itself shows a simple chat between the HTML client and the
C++/Qt server, with a line edit for input and a text edit showing the
chat history.
Change-Id: I8baf14efb9d0c5f5880d99710cf6317fe9b887b9
Reviewed-by: Zeno Albisser <zeno.albisser@digia.com>
-rw-r--r-- | examples/examples.pro | 2 | ||||
-rw-r--r-- | examples/standalone/dialog.ui | 48 | ||||
-rw-r--r-- | examples/standalone/index.html | 58 | ||||
-rw-r--r-- | examples/standalone/main.cpp | 128 | ||||
-rw-r--r-- | examples/standalone/standalone.pro | 11 | ||||
-rw-r--r-- | src/webchannel/qmetaobjectpublisher.cpp | 12 | ||||
-rw-r--r-- | src/webchannel/qmetaobjectpublisher.h | 9 |
7 files changed, 268 insertions, 0 deletions
diff --git a/examples/examples.pro b/examples/examples.pro index decf465..554ec11 100644 --- a/examples/examples.pro +++ b/examples/examples.pro @@ -1,5 +1,7 @@ TEMPLATE = subdirs +SUBDIRS += standalone + qtHaveModule(quick) { SUBDIRS += \ hybridshell \ diff --git a/examples/standalone/dialog.ui b/examples/standalone/dialog.ui new file mode 100644 index 0000000..056a3f5 --- /dev/null +++ b/examples/standalone/dialog.ui @@ -0,0 +1,48 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>Dialog</class> + <widget class="QDialog" name="Dialog"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>400</width> + <height>300</height> + </rect> + </property> + <property name="windowTitle"> + <string>Dialog</string> + </property> + <layout class="QGridLayout" name="gridLayout"> + <item row="1" column="0"> + <widget class="QLineEdit" name="input"> + <property name="placeholderText"> + <string>Message Contents</string> + </property> + </widget> + </item> + <item row="1" column="1"> + <widget class="QPushButton" name="send"> + <property name="text"> + <string>Send</string> + </property> + </widget> + </item> + <item row="0" column="0" colspan="2"> + <widget class="QPlainTextEdit" name="output"> + <property name="readOnly"> + <bool>true</bool> + </property> + <property name="plainText"> + <string notr="true">Initializing WebChannel...</string> + </property> + <property name="backgroundVisible"> + <bool>false</bool> + </property> + </widget> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/examples/standalone/index.html b/examples/standalone/index.html new file mode 100644 index 0000000..5efee77 --- /dev/null +++ b/examples/standalone/index.html @@ -0,0 +1,58 @@ +<!DOCTYPE html> +<html> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <script type="text/javascript" src="../../src/webchannel/webchannel.js"></script> + <script type="text/javascript" src="../../src/webchannel/qobject.js"></script> + <script type="text/javascript"> + //BEGIN SETUP + var baseUrl = (/[?&]webChannelBaseUrl=([A-Za-z0-9\-:/\.]+)/.exec(location.search)[1]); + new QWebChannel(baseUrl, function(channel) { + setupQObjectWebChannel(channel, function() { + document.getElementById("send").onclick = function() { + var input = document.getElementById("input"); + var text = input.value; + if (!text) { + return; + } + var output = document.getElementById("output"); + output.innerHTML = output.innerHTML + "Send message: " + text + "\n"; + input.value = ""; + dialog.receiveText(text); + } + + dialog.sendText.connect(function(text) { + var output = document.getElementById("output"); + output.innerHTML = output.innerHTML + "Received message: " + text + "\n"; + }); + }); + }); + + window.onload = function() { + } + //END SETUP + </script> + <style type="text/css"> + html { + height: 100%; + width: 100%; + } + #input { + width: 400px; + margin: 0 10px 0 0; + } + #send { + width: 90px; + margin: 0; + } + #output { + width: 500px; + height: 300px; + } + </style> + </head> + <body> + <textarea id="output"></textarea><br /> + <input id="input" /><input type="submit" id="send" value="Send" onclick="javascript:click();" /> + </body> +</html> diff --git a/examples/standalone/main.cpp b/examples/standalone/main.cpp new file mode 100644 index 0000000..99e325b --- /dev/null +++ b/examples/standalone/main.cpp @@ -0,0 +1,128 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2013 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Milian Wolff <milian.wolff@kdab.com> +** Contact: http://www.qt-project.org/legal +* +** This file is part of the QtWebChannel module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qwebchannel.h" +#include "qmetaobjectpublisher.h" + +#include <QApplication> +#include <QDialog> +#include <QVariantMap> +#include <QDesktopServices> +#include <QUrl> +#include <QDebug> + +#include "ui_dialog.h" + +class Dialog : public QObject +{ + Q_OBJECT + +public: + explicit Dialog(QWebChannel *channel, QObject *parent = 0) + : QObject(parent) + { + ui.setupUi(&dialog); + dialog.show(); + + connect(ui.send, SIGNAL(clicked()), SLOT(clicked())); + connect(channel, SIGNAL(baseUrlChanged(QString)), + SLOT(baseUrlChanged(QString))); + } + +signals: + void sendText(const QString &text); + +public slots: + void receiveText(const QString &text) + { + ui.output->appendPlainText(tr("Received message: %1").arg(text)); + } + +private slots: + void clicked() + { + const QString text = ui.input->text(); + + if (text.isEmpty()) { + return; + } + + emit sendText(text); + ui.output->appendPlainText(tr("Sent message: %1").arg(text)); + + ui.input->clear(); + } + + void baseUrlChanged(const QString &baseUrl) + { + ui.output->appendPlainText(tr("Initialization complete, opening browser.")); + + QUrl url = QUrl::fromLocalFile(SOURCE_DIR "/index.html"); + url.setQuery(QStringLiteral("webChannelBaseUrl=") + baseUrl); + QDesktopServices::openUrl(url); + } + +private: + QDialog dialog; + Ui::Dialog ui; +}; + +int main(int argc, char** argv) +{ + QApplication app(argc, argv); + + QWebChannel channel; + QMetaObjectPublisher publisher; + publisher.setWebChannel(&channel); + QObject::connect(&channel, SIGNAL(rawMessageReceived(QString)), + &publisher, SLOT(handleRawMessage(QString))); + + Dialog* dialog = new Dialog(&channel); + + QVariantMap objects; + objects[QStringLiteral("dialog")] = QVariant::fromValue(dialog); + publisher.registerObjects(objects); + + return app.exec(); +} + +#include "main.moc" diff --git a/examples/standalone/standalone.pro b/examples/standalone/standalone.pro new file mode 100644 index 0000000..7ce0f2b --- /dev/null +++ b/examples/standalone/standalone.pro @@ -0,0 +1,11 @@ +QT += gui webchannel widgets + +CONFIG += warn_on + +SOURCES += \ + main.cpp + +FORMS += \ + dialog.ui + +DEFINES += "SOURCE_DIR=\"\\\""$$PWD"\\\"\"" diff --git a/src/webchannel/qmetaobjectpublisher.cpp b/src/webchannel/qmetaobjectpublisher.cpp index c141fb2..fcf809a 100644 --- a/src/webchannel/qmetaobjectpublisher.cpp +++ b/src/webchannel/qmetaobjectpublisher.cpp @@ -54,6 +54,7 @@ #include <QDebug> #include <QPointer> #include <QEvent> +#include <QJsonDocument> namespace { const QString KEY_SIGNALS = QStringLiteral("signals"); @@ -647,6 +648,17 @@ bool QMetaObjectPublisher::handleRequest(const QJsonObject &message) return false; } +void QMetaObjectPublisher::handleRawMessage(const QString &message) +{ + QJsonParseError error; + const QJsonDocument doc = QJsonDocument::fromJson(message.toUtf8(), &error); + if (error.error) { + qWarning() << "Could not parse raw input message as JSON: " << error.errorString() << "Message was: " << message; + } else if (doc.isObject() && !handleRequest(doc.object())) { + qWarning() << "Could not handle raw message as meta object request: " << message; + } +} + QWebChannel *QMetaObjectPublisher::webChannel() const { return d->webChannel; diff --git a/src/webchannel/qmetaobjectpublisher.h b/src/webchannel/qmetaobjectpublisher.h index da733b2..684be1d 100644 --- a/src/webchannel/qmetaobjectpublisher.h +++ b/src/webchannel/qmetaobjectpublisher.h @@ -103,6 +103,15 @@ signals: void webChannelChanged(QWebChannel *channel); void blockUpdatesChanged(bool block); +public slots: + /** + * Helper slot which you can connect directly to WebChannel's rawMessageReceived signal. + * + * This slot then tries to parse the message as JSON and if it succeeds, calls handleRequest + * with the obtained JSON object. + */ + void handleRawMessage(const QString &message); + protected: void timerEvent(QTimerEvent *) Q_DECL_OVERRIDE; |