我们可以在 QML 文件中声明全局变量吗? [英] Can we declare global variable in QML file?

查看:58
本文介绍了我们可以在 QML 文件中声明全局变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一些类似于以下代码的事情:

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

我们可以在 QML 文件 中拥有全局变量并从 JavaScript 函数中访问它吗?

Can we have global variable in QML file and access it from the JavaScript function?

推荐答案

试试 property int globalForJs: 10;

如果你想要一个可以采用任何类型的变量:

If you want a variable that can take any type:

属性变量 globalForJs: 10

在 QML 2 之前,使用 variant 关键字代替 var.

Prior to QML 2, use the variant keyword instead of var.

这篇关于我们可以在 QML 文件中声明全局变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆