1. 程式人生 > >qml中TextField實現新增文字的上限,同時新增文字時,提示文字消失。

qml中TextField實現新增文字的上限,同時新增文字時,提示文字消失。

這裡寫圖片描述
當點選請添寫工具名稱時,游標移動到如下:
這裡寫圖片描述
新增文字後,請填寫工具名稱消失:
這裡寫圖片描述
同時設定新增文字的上限是31個字元,當超過31個就不能再新增。
相關程式碼:

import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.4

Rectangle{
    id:toolsDisp;
    width:250;
    height:60;


//設定工具名稱
    function setTaskName( name ){
       taskName.text = name ;
} function getToolsName(){ return toolsName.text; } function setToolName(name){ toolsName.text=name; } Row{ spacing: 10; anchors.top: parent.top; anchors.topMargin: 10; Column{ id:taskNameColumn Text{ id:taskNameHead text:"任務"
font.pointSize: 12 font.bold: true } Text{ id:taskName; anchors.top: taskNameHead.bottom anchors.topMargin: 5 text:"模板1" font.pointSize
: 8 color: "#313a4b" } } Column{ id:toolNameColumn; anchors.left: taskNameColumn.right anchors.leftMargin: 100 Text{ id:toolsNameHead; text:"工具名稱" font.pointSize: 12 font.bold: true } TextField{ id:toolsName; //text: "請填寫工具名稱" placeholderText: "請填寫工具名稱" font.pointSize: 8 textColor:"#313a4b" maximumLength:31 style: TextFieldStyle{ background: Rectangle{ opacity: 1; } } } } }