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
src_script_qjsengine.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
8
9
11QJSValue fun = myEngine.evaluate("(function(a, b) { return a + b; })");
13args << 1 << 2;
14QJSValue threeAgain = fun.call(args);
16
17
19QString fileName = "helloworld.qs";
20QFile scriptFile(fileName);
21if (!scriptFile.open(QIODevice::ReadOnly))
22 // handle error
23QTextStream stream(&scriptFile);
24QString contents = stream.readAll();
25scriptFile.close();
28
29
31myEngine.globalObject().setProperty("myNumber", 123);
32...
33QJSValue myNumberPlusOne = myEngine.evaluate("myNumber + 1");
35
36
39if (result.isError())
40 qDebug()
41 << "Uncaught exception at line"
42 << result.property("lineNumber").toInt()
43 << ":" << result.toString();
45
46
49QJSValue scriptButton = myEngine.newQObject(button);
50myEngine.globalObject().setProperty("button", scriptButton);
51
52myEngine.evaluate("button.checkable = true");
53
54qDebug() << scriptButton.property("checkable").toBool();
55scriptButton.property("show").call(); // call the show() slot
57
58
61
62QObject *myQObject = new QObject();
63myQObject->setProperty("dynamicProperty", 3);
64
67
68qDebug() << engine.evaluate("myObject.dynamicProperty").toInt();
70
71
73class MyObject : public QObject
74{
76
77public:
79};
81
83QJSValue jsMetaObject = engine.newQMetaObject(&MyObject::staticMetaObject);
86
88engine.evaluate("var myObject = new MyObject()");
Q_INVOKABLE MyObject()
\inmodule QtCore
Definition qfile.h:93
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...
QJSValue newQMetaObject(const QMetaObject *metaObject)
The QJSValue class acts as a container for Qt/JavaScript data types.
Definition qjsvalue.h:31
bool toBool() const
Returns the boolean value of this QJSValue, using the conversion rules described in \l{ECMA-262} sect...
Definition qjsvalue.cpp:546
QJSValue call(const QJSValueList &args=QJSValueList()) const
Calls this QJSValue as a function, passing args as arguments to the function, and using the globalObj...
Definition qjsvalue.cpp:705
qint32 toInt() const
Returns the signed 32-bit integer value of this QJSValue, using the conversion rules described in \l{...
Definition qjsvalue.cpp:566
void setProperty(const QString &name, const QJSValue &value)
Sets the value of this QJSValue's property with the given name to the given value.
QJSValue property(const QString &name) const
Returns the value of this QJSValue's property with the given name.
Definition qlist.h:75
\inmodule QtCore
Definition qobject.h:103
The QPushButton widget provides a command button.
Definition qpushbutton.h:20
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
QPushButton
[1]
QPushButton * button
[2]
EGLStreamKHR stream
GLuint64EXT * result
[6]
#define Q_OBJECT
#define Q_INVOKABLE
QJSValue fun
[0]
qDebug()<< engine.evaluate("myObject.dynamicProperty").toInt()
[1]
QJSValue three
QJSValue myScriptQObject
QJSValueList args
QJSEngine myEngine
[0]
QJSValue jsMetaObject
[7]
QJSEngine engine
[0]