QML 学习摘录 03 - 输入元素

QML输入元素 Input Element

完整文档参考:https://github.com/cwc1987/QmlBook-In-Chinese

1. 文本输入框TextInput

⽂本输⼊允许⽤户输⼊⼀⾏⽂本,可以通过点击TextInput来改变焦点。为了⽀持键盘改变焦点,我们可以使⽤KeyNavigation(按键向导)这个附加属性。

示例代码:TextInput组件 MyInput.qml

import QtQuick 2.3
FocusScope {
    id: root
    width: 100;height: 20
    Rectangle {
        anchors.fill:parent
        color: "lightblue"
        border.color: "gray"
    }
    property alias text: input.text
    property alias input: input
    TextInput {
        id: input
        anchors.fill: parent
        focus: true
        color: "red"
        text: "Please input.."
    }
}

焦点区域(FocusScope)定义了如果焦点区域接收到焦点,它的最后⼀个使⽤focus:true的⼦元素接收焦点,它将会把焦点传递给最后申请焦点的⼦元素。代码使⽤焦点区域(FocusScope)作为根元素。

2. 文本编辑框TextEdit

⽂本编辑(TextEdit)元素与TextInput⾮常类似,它⽀持多⾏⽂本编辑。它不再⽀持⽂本输⼊的限制,但是提供了已绘制⽂本的⼤⼩查询(paintedHeight,paintedWidth)。

示例代码:TextEdit组件 MyEdit.qml

import QtQuick 2.3
FocusScope {
    id: root
    width: 520;height: 400
    Rectangle{
        anchors.fill:parent
        color: "lightblue"
        border.color: "gray"
    }
    property alias text:input.text
    property alias input:input
    TextEdit{
        id:input
        anchors.fill: parent
        focus:true
        color: "red"
        text:"Please input.."
     }
 }

3. 按键元素

附加属性Key允许你基于某个按键的点击来执⾏代码。例如使⽤up,down按键来移动⼀个⽅块,left,right按键来旋转⼀个元素,plus,minus按键来缩放⼀个元素。

示例代码 : 演示TextInput、TextEdit的使用和Key属性

import QtQuick 2.3
Rectangle {
    id: root
    width: 800;height: 800
    border.color: "gray"
    Myinput {
        id: input
        x: 20;y: 10
        text: "组件:input1"
        KeyNavigation.tab: input2
    }
    Myinput {
        id: input2
        x: 20;y: 50
        text: "组件:input2"
        KeyNavigation.tab: input
    }
    MyEdit{
        id:input3
        x:20;y:80
        text:"输入你的文本..."
    }
    Rectangle{
        id:dragSquare
        x:200;y:10
        width: 40;height: 50
        color:"red"
        Drag.active:dragArea.drag.active
         MouseArea{
            id:dragArea
            anchors.fill: parent
            drag.target: parent
            onClicked:{dragSquare.focus = true}
         }
        focus:true
        /****控制矩形dragSquare的移动*****/
        Keys.onLeftPressed: dragSquare.x -=8
        Keys.onRightPressed: dragSquare.x +=8
        Keys.onDownPressed: dragSquare.y +=8
        Keys.onUpPressed: dragSquare.y -=8
        Keys.enabled: true //激活按键,否则下面代码无效
        Keys.onPressed:{
            switch(event.key)
            {
                case Qt.Key_Plus:
                    dragSquare.scale += 0.2;//dragSquare放大
                    break;
                case Qt.Key_Minus:
                    dragSquare.scale -= 0.2;//dragSquare缩小
                    break;
            }
        }
    }
}

运行效果:两个TextInput,可通过Tab键切换;红色矩形可拖拽,并通过方向按键移动或者+/- 缩放。
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值