Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
4//! [0]
6QJSValue three = myEngine.evaluate("1 + 2");
7//! [0]
8
9
10//! [1]
11QJSValue fun = myEngine.evaluate("(function(a, b) { return a + b; })");
13args << 1 << 2;
14QJSValue threeAgain = fun.call(args);
15//! [1]
16
17
18//! [2]
19QString fileName = "helloworld.qs";
20QFile scriptFile(fileName);
21if (!scriptFile.open(QIODevice::ReadOnly))
22 // handle error
23QTextStream stream(&scriptFile);
24QString contents = stream.readAll();
25scriptFile.close();
26myEngine.evaluate(contents, fileName);
27//! [2]
28
29
30//! [3]
31myEngine.globalObject().setProperty("myNumber", 123);
32...
33QJSValue myNumberPlusOne = myEngine.evaluate("myNumber + 1");
34//! [3]
35
36
37//! [4]
38QJSValue result = myEngine.evaluate(...);
39if (result.isError())
40 qDebug()
41 << "Uncaught exception at line"
42 << result.property("lineNumber").toInt()
43 << ":" << result.toString();
44//! [4]
45
46
47//! [5]
48QPushButton *button = new QPushButton;
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
56//! [5]
57
58
59//! [6]
61
62QObject *myQObject = new QObject();
63myQObject->setProperty("dynamicProperty", 3);
64
65QJSValue myScriptQObject = engine.newQObject(myQObject);
66engine.globalObject().setProperty("myObject", myScriptQObject);
67
68qDebug() << engine.evaluate("myObject.dynamicProperty").toInt();
69//! [6]
70
71
72//! [7]
73class MyObject : public QObject
74{
76
77public:
79};
80//! [7]
81
82//! [8]
83QJSValue jsMetaObject = engine.newQMetaObject(&MyObject::staticMetaObject);
84engine.globalObject().setProperty("MyObject", jsMetaObject);
85//! [8]
86
87//! [9]
88engine.evaluate("var myObject = new MyObject()");
89//! [9]
The QJSValue class acts as a container for Qt/JavaScript data types.
Definition qjsvalue.h:31
QString three
QJSValue fun
[0]
QJSValue myScriptQObject
QJSValueList args
QJSEngine myEngine
[0]
QJSValue jsMetaObject
[7]
QJSEngine engine
[0]