1. 程式人生 > >Qt工作筆記-QML中TextInput設定預設值,以及使用正則表示式只能輸入整數

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]/}
    }