Qt工作笔记-QML中TextInput设置默认值,以及使用正则表达式只能输入整数

程序运行截图如下:

源码如下:

import QtQuick 2.5
import QtQuick.Window 2.2

Window {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    Rectangle{
        width:parent.width
        height:8+14
        color:"lightsteelblue"
        border.color: "gray"
    }
    TextInput{
        height:8
        anchors.fill: parent
        font.pixelSize: 22
        focus:true
    }
}

如果要设置默认值:

就加一个text属性,代码如下:

    TextInput{
        height:8
        text:"500"
        anchors.fill: parent
        font.pixelSize: 22
        focus:true
    }

运行截图如下:

最简单的正则表达式:

1.只能输入2位整数:

    TextInput{
        height:8
        text:"1"
        anchors.fill: parent
        font.pixelSize: 22
        focus:true
        validator: RegExpValidator{regExp:/[0-9][0-9]/}
    }

猜你喜欢

转载自blog.csdn.net/qq78442761/article/details/81281201