From 1701720dc6c2df030ee5f7db4d8aa6e2422654db Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Thu, 19 Dec 2013 14:26:02 +0100 Subject: 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 --- examples/examples.pro | 2 + examples/standalone/dialog.ui | 48 ++++++++++++++ examples/standalone/index.html | 58 +++++++++++++++++ examples/standalone/main.cpp | 128 +++++++++++++++++++++++++++++++++++++ examples/standalone/standalone.pro | 11 ++++ 5 files changed, 247 insertions(+) create mode 100644 examples/standalone/dialog.ui create mode 100644 examples/standalone/index.html create mode 100644 examples/standalone/main.cpp create mode 100644 examples/standalone/standalone.pro (limited to 'examples') 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 @@ + + + Dialog + + + + 0 + 0 + 400 + 300 + + + + Dialog + + + + + + Message Contents + + + + + + + Send + + + + + + + true + + + Initializing WebChannel... + + + false + + + + + + + + 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 @@ + + + + + + + + + + +
+ + + 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 +** 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 +#include +#include +#include +#include +#include + +#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"\\\"\"" -- cgit v1.2.1