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?
To create a "global" variable, you need to make it available to everyone and you need to make it declared once, and only once. #include "globals.
Variables can be declared by var, let, and const keywords.
Global ScopeGlobal variables can be accessed from anywhere in a JavaScript program. Variables declared with var , let and const are quite similar when declared outside a block.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With