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