QML小程序:输入元素

展示自定义组件(行编辑框)的输入,焦点切换
展示自定义组件(文本编辑框)的输入
展示图如下:
在这里插入图片描述
main.qml

import QtQuick 2.12
import QtQuick.Window 2.12

//展示自定义组件(行编辑框)的输入,焦点切换
//展示自定义组件(文本编辑框)的输入

//窗口
Window {
    visible: true   //窗口可见
    width: 640  //窗口宽度
    height: 480 //窗口高度
    title: qsTr("Input")    //窗口标题

    //行编辑区域
    Rectangle {
        id: lineEditScope
        width: 200  //行编辑区域宽度
        height: 80  //行编辑区域高度
        color: "gray"   //行编辑区域颜色

        //自定义组件:行编辑框1
        CLineEdit {
            id: input1  //行编辑框1的id
            x: 8; y: 8  //行编辑框1位置
            width: 96   //行编辑框1宽度
            height: 20  //行编辑框1高度
            focus: true //行编辑框1焦点
            text: "Text Input 1"    //行编辑框1默认值
            KeyNavigation.tab: input2   //允许按Tab键切换焦点到input2
        }

        //注意:如果自定义组件中要使得KeyNavigation.tab生效,必须使用FocusScope(焦点区域)

        //自定义组件:行编辑框2
        CLineEdit {
            id: input2  //行编辑框2的id
            x: 8; y: 36  //行编辑框2位置
            width: 96   //行编辑框2宽度
            height: 20  //行编辑框2高度
            focus: true //行编辑框2焦点
            text: "Text Input 2"    //行编辑框2默认值
            KeyNavigation.tab: input1   //允许按Tab键切换焦点到input1
        }
    }

    //文本编辑区域
    Rectangle {
        id:textEditScope    //文本编辑区域id
        x:0; y: lineEditScope.height + 10   //文本编辑区域位置
        width: 138  //文本编辑区域宽度
        height: 118 //文本编辑区域高度
        color: "linen"  //文本编辑区域颜色

        //自定义组件:文本编辑框
        CTextEdit {
            id: input   //文本编辑框id
            x: 8; y:8   //文本编辑框位置
            width: 120  //文本编辑框宽度
            height: 108 //文本编辑框高度
            focus: true //文本编辑框焦点
            text: "Text Edit"   //文本编辑框默认值
        }
    }
}

CLineEdit.qml

import QtQuick 2.0

//自定义组件,行编辑框

//焦点区域元素
FocusScope {
    width: 96   //焦点区域宽度
    height: input.height + 8    //焦点区域高度

    //矩形框
    Rectangle {
        anchors.fill: parent    //锚定填充整个父部件
        color: "lightsteelblue" //矩形框颜色
        border.color: "gray"    //矩形框边框颜色
    }

    property alias text: input.text //别名text,将内部嵌套的元素属性导出到外部使用
    property alias input: input //别名input,将内部嵌套的元素属性导出到外部使用

    //文本输入元素
    TextInput {
        id: input   //文本输入id
        anchors.fill: parent    //文本输入锚定填充到整个父部件
        anchors.margins: 4  //文本输入锚定边框间隔:4
        focus: true //焦点:开
    }
}

CTextEdit.qml

import QtQuick 2.0

//自定义组件,文本编辑框

//焦点区域元素
FocusScope {
    width: 96   //焦点区域宽度
    height: 96  //焦点区域高度

    //矩形框元素
    Rectangle {
        anchors.fill: parent    //锚定填充到整个父部件
        color: "lightsteelblue" //矩形框颜色
        border.color: "gray"    //矩形框边框颜色
    }
    property alias text: input.text //别名,将内部嵌套的元素属性导出到外部使用
    property alias input: input //别名,将内部嵌套的元素属性导出到外部使用

    //文本编辑框
    TextEdit {
        id: input   //文本编辑框id
        anchors.fill: parent    //锚定填充到整个父部件
        anchors.margins: 4  //锚定文本编辑框边距间隔:4
        focus: true //焦点
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值