qml 自定义 LineEdit
import QtQuick 2.0
FocusScope{
id: wrapper
property DoubleValidator doubleVal:
DoubleValidator{
bottom: 10.00
top:50.01
decimals: 4 //保留小数点位数
//notation: DoubleValidator.StandardNotation
//notation: DoubleValidator.ScientificNotation
}
property string hintStr: "hello world"
Rectangle{
id: rect
width: 110
height: 39
//anchors.fill: parent
border.color: "#C1C1C1"
border.width: 1
color: "white"
radius: 2 //半径
TextInput{
id: input
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.left: parent.left;//iconArea.right
anchors.leftMargin: 6
anchors.right: parent.right
anchors.rightMargin: 9
horizontalAlignment: TextInput.AlignLeft
verticalAlignment: TextInput.AlignVCenter
font.pointSize: 14
color: "#444444"
clip: true
//只接受5-120之内的值
//validator: IntValidator{bottom: 5;top:120}
//使用外面设定的值
validator: doubleVal
Text {
id: placeHold
font: input.font
color: "#d0d0d0"
opacity: input.length ? 0 : 1 //当默认提示使用
Behavior on opacity{
NumberAnimation{ duration: 300 }
}
anchors.fill: parent
verticalAlignment: TextInput.AlignVCenter
text: hintStr //使用传进来的提示字符
}
}
}
}
使用例子:
LineEdit
{
x: 110
hintStr: qsTr("只能输数字")
doubleVal:
DoubleValidator{
bottom: 10.00
top:50.01
decimals: 5 //保留小数点位数
notation: DoubleValidator.StandardNotation
//notation: DoubleValidator.ScientificNotation
}
}