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
avatarExample.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
"avatarExample.h"
5
#
include
<
QQmlEngine
>
6
#
include
<
QQmlComponent
>
7
#
include
<
QGuiApplication
>
8
9
struct
Expectations
10
{
11
QQmlEngine
engine
;
12
13
void
expectOne
()
14
{
15
//![1]
16
QQmlComponent component(&engine,
"qrc:/exampleOne.qml"
);
17
QObject *object = component.create();
18
// The scarce resource will have been released automatically
19
// by this point, after the binding expression was evaluated.
20
bool
expectedResult = (object->property(
"avatarWidth"
).toInt() == 100);
21
delete
object;
22
//![1]
23
Q_ASSERT(expectedResult);
24
}
25
26
void
expectTwo
()
27
{
28
//![2]
29
QQmlComponent component(&engine,
"qrc:/exampleTwo.qml"
);
30
QObject *object = component.create();
31
// The scarce resource will not have been released automatically
32
// after the binding expression was evaluated.
33
// Since the scarce resource was not released explicitly prior
34
// to the binding expression being evaluated, we get:
35
bool
expectedResult = (object->property(
"avatar"
).isValid() ==
true
);
36
delete
object;
37
//![2]
38
Q_ASSERT(expectedResult);
39
}
40
41
void
expectThree
()
42
{
43
//![3]
44
QQmlComponent component(&engine,
"qrc:/exampleThree.qml"
);
45
QObject *object = component.create();
46
// The resource was preserved explicitly during evaluation of the
47
// JavaScript expression. Thus, during property assignment, the
48
// scarce resource was still valid, and so we get:
49
bool
expectedResult = (object->property(
"avatar"
).isValid() ==
true
);
50
// The scarce resource will not be released until all references to
51
// the resource are released, and the JavaScript garbage collector runs.
52
delete
object;
53
//![3]
54
Q_ASSERT(expectedResult);
55
}
56
57
void
expectFour
()
58
{
59
//![4]
60
QQmlComponent component(&engine,
"qrc:/exampleFour.qml"
);
61
QObject *object = component.create();
62
// The scarce resource was explicitly preserved by the client during
63
// the importAvatar() function, and so the scarce resource
64
// remains valid until the explicit call to releaseAvatar(). As such,
65
// we get the expected results:
66
bool
expectedResultOne = (object->property(
"avatarOne"
).isValid() ==
true
);
67
bool
expectedResultTwo = (object->property(
"avatarTwo"
).isValid() ==
false
);
68
// Because the scarce resource referenced by avatarTwo was released explicitly,
69
// it will no longer be consuming any system resources (beyond what a normal
70
// JS Object would; that small overhead will exist until the JS GC runs, as per
71
// any other JavaScript object).
72
delete
object;
73
//![4]
74
Q_ASSERT(expectedResultOne);
75
Q_ASSERT(expectedResultTwo);
76
}
77
78
void
expectFive
()
79
{
80
//![5]
81
QQmlComponent component(&engine,
"qrc:/exampleFive.qml"
);
82
QObject *object = component.create();
83
// We have the expected results:
84
bool
expectedResultOne = (object->property(
"avatarOne"
).isValid() ==
false
);
85
bool
expectedResultTwo = (object->property(
"avatarTwo"
).isValid() ==
false
);
86
// Because although only avatarTwo was explicitly released,
87
// avatarOne and avatarTwo were referencing the same
88
// scarce resource.
89
delete
object;
90
//![5]
91
Q_ASSERT(expectedResultOne);
92
Q_ASSERT(expectedResultTwo);
93
}
94
};
95
96
int
main
(
int
argc,
char
*argv[])
97
{
98
QGuiApplication app(argc, argv);
99
Expectations
expectations;
100
expectations
.
expectOne
(
)
;
101
expectations
.
expectTwo
(
)
;
102
expectations
.
expectThree
(
)
;
103
expectations
.
expectFour
(
)
;
104
expectations
.
expectFive
(
)
;
105
}
main
int main(int argc, char *argv[])
[ctor_close]
Definition
src_corelib_animation_qpropertyanimation.cpp:100
Expectations
Definition
avatarExample.cpp:10
Expectations::expectFive
void expectFive()
Definition
avatarExample.cpp:78
Expectations::expectThree
void expectThree()
Definition
avatarExample.cpp:41
Expectations::engine
QQmlEngine engine
Definition
avatarExample.cpp:11
Expectations::expectTwo
void expectTwo()
Definition
avatarExample.cpp:26
Expectations::expectFour
void expectFour()
Definition
avatarExample.cpp:57
Expectations::expectOne
void expectOne()
Definition
avatarExample.cpp:13
qtdeclarative
src
qml
doc
snippets
qml
integrating-javascript
scarceresources
avatarExample.cpp
Generated on
for Qt by
1.18.0