QML -->变量和函数

1、在qml中定义变量的方法:

property int spacePresses: 0  

 

其中property为关键字, int为类型, spacePresses为变量名, 0表示值

 

2、定义函数的方法

function increment(){
    spacePresses = spacePresses + 1
}

其中function为关键字, increment为函数名

 

3、按键

Keys

Keys.onSpacePressed:{}  空格键按下

Keys.onEscapePressed:{}  esc键被按下

 

4、文本改变

onTextChanged: console.log("text change to:", text)

 

5、使用:

import QtQuick 2.0

Rectangle{
    id: root
    width: 400
    height: 300

    Text {
        id: label
        x:24
        y:24
        property int spacePresses: 0
        text: "Space pressed: " + spacePresses + " times"
        onTextChanged: console.log("text change to:", text)
        focus: true
        Keys.onSpacePressed:{
            increment()
        }
        Keys.onEscapePressed:{
            label.text = ''
        }

        function increment(){
            spacePresses = spacePresses + 1
        }
    }
}

1、实现功能是,点击空格时,文本内容改变,调用函数并打印内容。点击esc时,清楚文本内容,但是由于文本发生了改变,所以也会打印。

结果展示:

 

 点击空格时

 

 点击esc时:

 

posted on 2021-08-03 15:58  缘随风烬  阅读(2342)  评论(0编辑  收藏  举报