• QML-编辑框的使用


    一、TextInput

    Textinput 类似于 QLineEdit,除了显示光标和文本外,默认并没有边框等装饰性效果,所以在使用时一般要为其添加Rectange可视化的外观。

    在这里插入图片描述

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Controls 2.1
    import QtQuick.Controls.Styles 1.2
    import QtQuick.Layouts 1.2
    import QtQuick.Dialogs 1.2
    
    Window {
        id:root
        visible: true
        width: 600
        height: 500
        title: qsTr("文本编辑框")
    
        Label{
            id:label_1
            text: qsTr("请输入:")
            anchors.centerIn: parent
            font.pointSize: 15
        }
    
        Rectangle{
    
            // 这里添加自适应,要不让输入框字符串过多的时候
            // 显示会超出编辑框
            width: input.contentWidth<100 ? 100 : input.contentWidth + 10
            height: input.contentHeight + 5
    
            color: "lightgrey"
            border.color: "grey"
            anchors.left: label_1.right
            anchors.top: label_1.top
    
            TextInput{
                id:input
                anchors.fill: parent
                anchors.margins: 2
                font.pixelSize: 15
                focus: true
            }
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    1、添加自适应

    如果不添加自适应,输入字符串过多的情况下,字符会超出控件,现象如下
    在这里插入图片描述

    所以需要添加下面这个关键部分
    在这里插入图片描述

    2、设置掩码

    inputMask 来限制输入的内容,输入掩码就是使用一些特殊的字符来限制输入的格式和内容。
    在这里插入图片描述
    在这里插入图片描述

    3、添加密码显示

    echoMode 属性指定了文本的显示方式,可用的方式有:

    TextInput.Normal:直接显示文本(默认方式);
    TextInput.Password:使用密码掩码字符(根据不同平台显示效果不同)来代替真实的字符;
    TextInput.NoEcho:不显示输入的内容;
    TextInput.PasswordEchoOnEdit:使用密码掩码字符,但在输入时显示真实字符。

    在这里插入图片描述

    4、编辑完成事件

    在这里插入图片描述

    5、添加过滤

    使用正则表达式,也可以使用qt自带的,例如:
    IntValidator 、DoubleValidator(非整数验证器)和RegExpValidator(正则表达式验证器)。下面的代码可以限制在 TextInput 中只能输入 0到 1000之间的整数:

    在这里插入图片描述

    二、TextField

    TextField使用起来比较直接,也是推荐使用这个,不需要你过多重新造轮子
    在这里插入图片描述

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Controls 2.1
    import QtQuick.Controls.Styles 1.2
    import QtQuick.Layouts 1.2
    import QtQuick.Dialogs 1.2
    
    Window {
        id:root
        visible: true
        width: 500
        height: 200
        title: qsTr("文本编辑框")
    
    Row{
        anchors.centerIn:parent
        spacing: 20
    
        Label{
            id:label_1
            text: qsTr("请输入:")
            font.pointSize: 15
        }
    
        TextField{
            id:textFiled_1
            placeholderText: qsTr("输入你个人密码")
            font.pointSize: 15
            background: Rectangle{
                implicitHeight: 24
                implicitWidth: 200
                color: "grey"
                border.color: "black"
            }
    
        }
    
    }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
  • 相关阅读:
    TCP过程中,网络断开问题解决办法
    JS 数组的排序 sort方法详解
    Pygame之滑稽球壁碰
    09.Oracle表的分区
    安卓应用程序打包时超过64K限制会出现什么问题?如何解决这个问题?
    java代码proguard代码混淆GUI使用,附带混淆map映射
    Go实现安全双检锁的方法和最佳实践
    【R语言】线性混合模型进行重复测量设计分析
    docker安装常用软件
    -EasyUI-------BootStrap,layer-已经谢幕了,为了感谢曾经相遇,说说里面个人认为比较好的功能吧
  • 原文地址:https://blog.csdn.net/u013083044/article/details/126096691