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
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
{
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
;
23
Q_ASSERT
(expectedResult);
24
}
25
26
void
expectTwo
()
27
{
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
;
38
Q_ASSERT
(expectedResult);
39
}
40
41
void
expectThree
()
42
{
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
;
54
Q_ASSERT
(expectedResult);
55
}
56
57
void
expectFour
()
58
{
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
;
74
Q_ASSERT
(expectedResultOne);
75
Q_ASSERT
(expectedResultTwo);
76
}
77
78
void
expectFive
()
79
{
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
;
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
}
avatarExample.h
QGuiApplication
\macro qGuiApp
Definition
qguiapplication.h:37
QObject
\inmodule QtCore
Definition
qobject.h:103
QQmlComponent
The QQmlComponent class encapsulates a QML component definition.
Definition
qqmlcomponent.h:32
QQmlEngine
The QQmlEngine class provides an environment for instantiating QML components.
Definition
qqmlengine.h:57
main
int main()
[0]
Definition
doc_src_objecttrees.cpp:5
object
GLuint object
[3]
Definition
qopengles2ext.h:1248
component
static qreal component(const QPointF &point, unsigned int i)
Definition
qpathclipper.cpp:626
Q_ASSERT
#define Q_ASSERT(cond)
Definition
qrandom.cpp:47
app
QApplication app(argc, argv)
[0]
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 by
1.10.0