QT QML学习系列(七)-表达式和运算符

   在QML中,表达式是指由操作符、操作数和函数组成的语句。表达式可以用于计算值、比较值以及执行各种操作。QML中的运算符和其他编程语言中的运算符类似,包括算术运算符、关系运算符、逻辑运算符等。

5.1 表达式语法

QML的表达式语法非常灵活,允许使用各种运算符和操作数。下面是一些基本的表达式语法规则:

  • 表达式由操作符和操作数组成。
  • 操作数可以是字面量、变量或函数调用。
  • 表达式可以由一个或多个子表达式组成。
  • 可以使用括号来明确指定表达式的计算顺序。

5.2 运算符分类

QML的运算符主要分为以下几类:

  • 算术运算符:用于执行基本的算术运算,如加、减、乘、除、取余等。例如:+、-、*、/、%。
  • 关系运算符:用于比较两个值之间的关系,如相等、大于、小于等。例如:==、!=、>、<、>=、<=。
  • 逻辑运算符:用于执行布尔运算,如与、或、非等。例如:&&、||、!。
  • 位运算符:用于操作二进制数的位。例如:&、|、~、^、<<、>>。
  • 三目运算符:用于根据条件执行不同的操作。例如:condition ? value1 : value2。

5.3 运算符优先级

在QML中,不同类型的运算符具有不同的优先级。运算符的优先级指定了表达式中各个运算符的计算顺序。例如,乘法运算符的优先级高于加法运算符,因此乘法运算先于加法运算执行。

以下是QML中常见运算符的优先级,从高到低:

  1. 括号:()
  2. 一元运算符:!、~、-、+
  3. 乘法运算符:*、/、%
  4. 加法运算符:+、-
  5. 移位运算符:<<、>>、>>>
  6. 按位与运算符:&
  7. 按位异或运算符:^
  8. 按位或运算符:|
  9. 关系运算符:==、!=、<、<=、>、>=、in、instanceof
  10. 逻辑与运算符:&&
  11. 逻辑或运算符:||
  12. 三目运算符:? :

QML的运算符优先级与其他编程语言的优先级类

Rectangle {
    width: 200
    height: 200
    color: "lightblue"

    // 定义变量
    property int num1: 10
    property int num2: 5
    property string str1: "Hello"
    property string str2: "QML"

    // 算术运算符示例
    Text {
        text: "num1 + num2 = " + (num1 + num2) // 输出15
        anchors.top: parent.top
        anchors.horizontalCenter: parent.horizontalCenter
    }
    Text {
        text: "num1 - num2 = " + (num1 - num2) // 输出5
        anchors.top: text1.bottom
        anchors.horizontalCenter: parent.horizontalCenter
    }
    Text {
        text: "num1 * num2 = " + (num1 * num2) // 输出50
        anchors.top: text2.bottom
        anchors.horizontalCenter: parent.horizontalCenter
    }
    Text {
        text: "num1 / num2 = " + (num1 / num2) // 输出2
        anchors.top: text3.bottom
        anchors.horizontalCenter: parent.horizontalCenter
    }
    Text {
        text: "num1 % num2 = " + (num1 % num2) // 输出0
        anchors.top: text4.bottom
        anchors.horizontalCenter: parent.horizontalCenter
    }

    // 关系运算符示例
    Text {
        text: "num1 == num2: " + (num1 == num2) // 输出false
        anchors.top: text5.bottom
        anchors.horizontalCenter: parent.horizontalCenter
    }
    Text {
        text: "num1 != num2: " + (num1 != num2) // 输出true
        anchors.top: text6.bottom
        anchors.horizontalCenter: parent.horizontalCenter
    }
    Text {
        text: "num1 > num2: " + (num1 > num2) // 输出true
        anchors.top: text7.bottom
        anchors.horizontalCenter: parent.horizontalCenter
    }
    Text {
        text: "num1 >= num2: " + (num1 >= num2) // 输出true
        anchors.top: text8.bottom
        anchors.horizontalCenter: parent.horizontalCenter
    }
    Text {
        text: "num1 < num2: " + (num1 < num2) // 输出false
        anchors.top: text9.bottom
        anchors.horizontalCenter: parent.horizontalCenter
    }
    Text {
        text: "num1 <= num2: " + (num1 <= num2) // 输出false
        anchors.top: text10.bottom
        anchors.horizontalCenter: parent.horizontalCenter
    }

    // 逻辑运算符示例
    Text {
        text: "num1 > 0 && num2 < 10: " + (num1 > 0 && num2 < 10) // 输出true
        anchors.top: text11.bottom
        anchors.horizontalCenter: parent.horizontalCenter
    }
    Text {
        text: "num1 > 0 || num2 < 10: " + (num1 > 0 || num2 < 10) // 输出true
        anchors.top: text12.bottom
        anchors.horizontalCenter: parent.horizontalCenter
    }
    Text {
        text: "! (num1 > 0): " + (! (num1 > 0)) // 输出false
        anchors.top: text13.bottom

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值