Overte C++ Documentation
QmlWrapper.h
1 //
2 // Created by Anthony J. Thibault on 2016-12-12
3 // Copyright 2013-2016 High Fidelity, Inc.
4 // Copyright 2023 Overte e.V.
5 //
6 // Distributed under the Apache License, Version 2.0.
7 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
8 // SPDX-License-Identifier: Apache-2.0
9 //
10 
11 #ifndef hifi_QmlWrapper_h
12 #define hifi_QmlWrapper_h
13 
14 #include <QtCore/QObject>
15 #include <QtCore/QVariant>
16 
17 #include <ScriptEngine.h>
18 #include <ScriptValue.h>
19 
20 class ScriptEngine;
21 
22 class QmlWrapper : public QObject {
23  Q_OBJECT
24 public:
25  QmlWrapper(QObject* qmlObject, QObject* parent = nullptr);
26 
27  Q_INVOKABLE void writeProperty(QString propertyName, QVariant propertyValue);
28  Q_INVOKABLE void writeProperties(QVariant propertyMap);
29  Q_INVOKABLE QVariant readProperty(const QString& propertyName);
30  Q_INVOKABLE QVariant readProperties(const QVariant& propertyList);
31 
32 protected:
33  QObject* _qmlObject{ nullptr };
34 };
35 
36 template <typename T>
37 ScriptValue wrapperToScriptValue(ScriptEngine* engine, T* const &in) {
38  if (!in) {
39  return engine->undefinedValue();
40  }
41  return engine->newQObject(in, ScriptEngine::QtOwnership);
42 }
43 
44 template <typename T>
45 bool wrapperFromScriptValue(const ScriptValue& value, T* &out) {
46  out = qobject_cast<T*>(value.toQObject());
47  return !!out;
48 }
49 
50 #endif
Provides an engine-independent interface for a scripting engine.
Definition: ScriptEngine.h:93
@ QtOwnership
Object is managed by Qt.
Definition: ScriptEngine.h:114
[ScriptInterface] Provides an engine-independent interface for QScriptValue
Definition: ScriptValue.h:40