From 4e1f78b585863990389bca2192b72650878111fb Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Fri, 4 Mar 2016 11:22:39 -0800 Subject: [PATCH] Allow message passing from a script to a QML window --- interface/resources/qml/QmlWindow.qml | 16 ++++++++++++++++ libraries/ui/src/QmlWindowClass.cpp | 7 +++++++ libraries/ui/src/QmlWindowClass.h | 6 ++++++ 3 files changed, 29 insertions(+) diff --git a/interface/resources/qml/QmlWindow.qml b/interface/resources/qml/QmlWindow.qml index d0f6edf9d1..ed8ff93f19 100644 --- a/interface/resources/qml/QmlWindow.qml +++ b/interface/resources/qml/QmlWindow.qml @@ -50,7 +50,23 @@ Windows.Window { } } } + + // Handle message traffic from the script that launched us to the loaded QML + function fromScript(message) { + if (root.dynamicContent && root.dynamicContent.fromScript) { + root.dynamicContent.fromScript(message); + } + } + // Handle message traffic from our loaded QML to the script that launched us + signal sendToScript(var message); + onDynamicContentChanged: { + if (dynamicContent && dynamicContent.sendToScript) { + dynamicContent.sendToScript.connect(sendToScript); + } + } + + Item { id: contentHolder anchors.fill: parent diff --git a/libraries/ui/src/QmlWindowClass.cpp b/libraries/ui/src/QmlWindowClass.cpp index 1b9880fa3c..5bd40b10a9 100644 --- a/libraries/ui/src/QmlWindowClass.cpp +++ b/libraries/ui/src/QmlWindowClass.cpp @@ -217,6 +217,13 @@ QmlWindowClass::QmlWindowClass(QObject* qmlWindow) qDebug() << "Created window with ID " << _windowId; Q_ASSERT(_qmlWindow); Q_ASSERT(dynamic_cast(_qmlWindow)); + // Forward messages received from QML on to the script + connect(_qmlWindow, SIGNAL(sendToScript(QVariant)), this, SIGNAL(fromQml(const QVariant&)), Qt::QueuedConnection); +} + +void QmlWindowClass::sendToQml(const QVariant& message) { + // Forward messages received from the script on to QML + QMetaObject::invokeMethod(asQuickItem(), "fromScript", Qt::QueuedConnection, Q_ARG(QVariant, message)); } QmlWindowClass::~QmlWindowClass() { diff --git a/libraries/ui/src/QmlWindowClass.h b/libraries/ui/src/QmlWindowClass.h index 2e848b612d..26152b1f24 100644 --- a/libraries/ui/src/QmlWindowClass.h +++ b/libraries/ui/src/QmlWindowClass.h @@ -67,17 +67,23 @@ public slots: void setTitle(const QString& title); + // Ugh.... do not want to do Q_INVOKABLE void raise(); Q_INVOKABLE void close(); Q_INVOKABLE int getWindowId() const { return _windowId; }; Q_INVOKABLE QmlScriptEventBridge* getEventBridge() const { return _eventBridge; }; + // Scripts can use this to send a message to the QML object + void sendToQml(const QVariant& message); + signals: void visibilityChanged(bool visible); // Tool window void moved(glm::vec2 position); void resized(QSizeF size); void closed(); + // Scripts can connect to this signal to receive messages from the QML object + void fromQml(const QVariant& message); protected slots: void hasClosed();