首页 > 其他分享 >更改通知语义:QML基本类型var

更改通知语义:QML基本类型var

时间:2023-02-19 13:56:36浏览次数:51  
标签:对象 语义 QML var property example 属性

注:百度整体翻译的很好,稍有更改

QML基本类型var

var类型为一个通用类型,和任何数据类型都有涉及。
它和JavaScript的规则变量var是相等的。例如,var特性可以存储数字,字符串,对象,数组和函数:

Item {
    property var aNumber: 100
    property var aBool: false
    property var aString: "Hello world!"
    property var anotherString: String("#FF008800")
    property var aColor: Qt.rgba(0.2, 0.3, 0.4, 0.5)
    property var aRect: Qt.rect(10, 10, 10, 10)
    property var aPoint: Qt.point(10, 10)
    property var aSize: Qt.size(10, 10)
    property var aVector3d: Qt.vector3d(100, 100, 100)
    property var anArray: [1, 2, 3, "four", "five", (function() { return "six"; })]
    property var anObject: { "foo": 10, "bar": 20 }
    property var aFunction: (function() { return "one"; })
}

说明:对于上面的var数组,还进行了混合存储,最后一个元素对象为函数,对函数对象进行访问要以函数调用形式,如anArray[5](), aFunction()。

更改通知

需要注意的是,分配为var特性的JavaScript对象的属性的更改不会触发有绑定访问的更新。由于对wheels属性的更改不会造成有绑定赋值关系的"text"属性的更新,则下面的例子仍然显示“The car has 4 wheels”:

Item {
    property var car: new Object({wheels: 4})

    Text {
        text: "The car has " + car.wheels + " wheels";
    }

    Component.onCompleted: {
        car.wheels = 6;
    }
}

如果onCompleted中处理改为"car = new Object({wheels: 6})",则text属性内容将会被更新为"The car has 6 wheels",因为car属性本身将被更改,这将导致发出更改通知。

属性值初始化

QML语法定义属性值初始化赋值右侧的大括号表示绑定赋值。当初始化var属性时,这可能会令人困惑,因为JavaScript中的空大括号可以表示表达式块或空对象声明。如果希望将var属性初始化为空对象值,则应将大括号括在括号中。
例如:

Item {
    property var first:  {}   // 空 = 未定义
    property var second: {{}} // 空表达式块 = 未定义
    property var third:  ({}) // 空对象
}

在前面的示例中,第一个属性绑定到一个空表达式,其结果未定义。第二个属性绑定到包含单个空表达式块("{}")的表达式,该表达式同样具有未定义的结果。第三个属性绑定到一个表达式,该表达式被计算为空对象声明,因此该属性将用该空对象值初始化。

类似地,JavaScript中的冒号可以是对象属性值赋值,也可以是代码标签。因此,使用对象声明初始化var属性也可能需要括号:

Item {
    property var first: { example: 'true' }    // example被解释为标签
    property var second: ({ example: 'true' }) // example被解释为对象的属性
    property var third: { 'example': 'true' }  // example被解释为属性
    Component.onCompleted: {
        console.log(first.example) // 打印'undefined', 因为"first"被赋值为string,正确操作应该是console.log(first)
        console.log(second.example) // prints 'true'
        console.log(third.example) // prints 'true'
    }
}

在最上面“QML基本类型var”的代码中,函数对象赋值绑定均被圆括号括起来;“anObject”对象赋值绑定,其大括号内冒号的左边也使用了引号(注:单引号、双引号等同效果)。

注意和补充:

  1. 属性初始化示例中,example被解释为标签的例子,不要因为大括号混淆了QML Type和QML类型var它们的属性初始化规则。如上面“更改通知”小节中QML Type的Text,其属性text没有引号,但该text是被定义在Text中,可以被访问到的。JavaScript中的标签与break或continue搭配使用,用来跳出循环,类似于C语言中的"goto 标签"。
  2. 函数对象外层的大括号可有可无。
  3. 函数中的语句末尾分号可有可无。
  4. 访问数组元素要使用中括号,中括号内填数组整数索引值。
  5. 对于var类型属性访问,可以用".",如second.example,也可以用"[]",如second["second"](或者second['second'])。

上面补充几点的用法主要是来自原链接:Loop on object property (QML),展示了遍历获取对象属性值,代码中有两点技巧:

  1. 一是使用"[]"符号结合语句"for (var x in greyButtonFunctions)"遍历了对象的所有属性值;
  2. 二是对象属性值又是一个函数对象,对其访问等于调用函数,执行了函数体。
import QtQuick 2.7
import QtQuick.Controls 2.0

Rectangle
{
    color: palette.grey

    property var spotButtonFunctions :
    {
        'EditViewTool' : function() {switchToEditViewToolButton.source = "../View/Icons/icon_game.png";},
        'SelectTool'   : function() {switchToSelectToolButton.source = "../View/Icons/icon_game.png";}
    }

    property var greyButtonFunctions :
    {
        'EditViewTool' : function() {switchToEditViewToolButton.source = "../View/Icons/icon_settings.png";},
        'SelectTool'   : function() {switchToSelectToolButton.source = "../View/Icons/icon_info.png";}
    }

    // View update slots.
    function onNotifyCurrentToolSignal(currentToolName)
    {
        // Grey all tool buttons.
        for (var x in greyButtonFunctions)
            greyButtonFunctions[x]();

        // Spot the current tool button.
        if (currentToolName !== "")
            spotButtonFunctions[currentToolName]();
    }
}

标签:对象,语义,QML,var,property,example,属性
From: https://www.cnblogs.com/2matoes/p/17114871.html

相关文章