Can we declare global variable in QML file?
24,831
Solution 1
Try property int globalForJs: 10;
If you want a variable that can take any type:
property var globalForJs: 10
Prior to QML 2, use the variant
keyword instead of var
.
Solution 2
Using int
or variant
properties do not create a javascript variable, but rather a semantically different generic QML property (see here)
Before Qt 5, global javascript variables were recommended to be defined in a separately imported javascript file, however Qt 5 adds a var
type property support.
Author by
psp1
Updated on November 18, 2020Comments
-
psp1 over 3 years
I want to do something similer to following code:
//test.qml import QtQuick 1.0 Item { var globalforJs =10; function increment() // JavaScript function { globalforJs++; } .... QML Code
Can we have global variable in
QML file
and access it from the JavaScript function? -
psp1 almost 13 yearsThanks for the reply, can this
property variant globalForJs: 10
can be accessed from the java script function which is in QML file itself? -
coyotte508 almost 13 yearsyes it can. All properties of a QML object can be accessed by any javascript function defined inside it.
-
KernelPanic over 8 yearsBut how to make this property
private
, so it is not seen by other QML stuff? -
coyotte508 over 8 yearsI don't think you can do that (javascript is not big on private member variables)