Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4#include <QObject>
5#include <QtQml>
6#include "myobject.h"
7
8int main(int argc, char *argv[])
9{
10 QCoreApplication app(argc, argv);
13 QObject *someObject = new MyObject;
14 QJSValue objectValue = engine.newQObject(someObject);
15 engine.globalObject().setProperty("myObject", objectValue);
17 qDebug() << "myObject's calculate() function returns"
18 << engine.evaluate("myObject.calculate(10)").toNumber();
19 return 0;
20}
\inmodule QtCore
The QJSEngine class provides an environment for evaluating JavaScript code.
Definition qjsengine.h:26
QJSValue globalObject() const
Returns this engine's Global Object.
QJSValue newQObject(QObject *object)
Creates a JavaScript object that wraps the given QObject object, using JavaScriptOwnership.
QJSValue evaluate(const QString &program, const QString &fileName=QString(), int lineNumber=1, QStringList *exceptionStackTrace=nullptr)
Evaluates program, using lineNumber as the base line number, and returns the result of the evaluation...
The QJSValue class acts as a container for Qt/JavaScript data types.
Definition qjsvalue.h:31
double toNumber() const
Returns the number value of this QJSValue, as defined in \l{ECMA-262} section 9.3,...
Definition qjsvalue.cpp:526
void setProperty(const QString &name, const QJSValue &value)
Sets the value of this QJSValue's property with the given name to the given value.
\inmodule QtCore
Definition qobject.h:103
int main()
[0]
#define qDebug
[1]
Definition qlogging.h:164
QApplication app(argc, argv)
[0]
QJSEngine engine
[0]