Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we declare global variable in QML file?

Tags:

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?

like image 359
psp1 Avatar asked Sep 22 '11 13:09

psp1


People also ask

How do you declare a global variable in Qt?

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.

What are the different ways to declare global variables?

Variables can be declared by var, let, and const keywords.

Can you set global variables in JavaScript?

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.


2 Answers

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.

like image 68
coyotte508 Avatar answered Sep 18 '22 17:09

coyotte508


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.

like image 41
Šarūnas Valaškevičius Avatar answered Sep 19 '22 17:09

Šarūnas Valaškevičius