Qt textfield的安全文本输入
我使用textfield输入带有echomode密码的密码。但我希望它应该显示字符,同时输入和一段时间后(一秒)或输入下一个字符,它应该转到星号 代码: 我尝试使用文本项来显示当前的回车键,过了一段时间,我将使其不可见,并根据光标位置移动文本项。 代码:Qt textfield的安全文本输入,qt,qml,qtquick2,Qt,Qml,Qtquick2,我使用textfield输入带有echomode密码的密码。但我希望它应该显示字符,同时输入和一段时间后(一秒)或输入下一个字符,它应该转到星号 代码: 我尝试使用文本项来显示当前的回车键,过了一段时间,我将使其不可见,并根据光标位置移动文本项。 代码: 问题:我仍然可以查看文本项后面的星号。谁能帮我解决这个问题。非常有趣的问题。但我认为用这种方式覆盖TextField的可见文本是不可能的。至于解决方案,我只能提供这个蹩脚的解决方案: TextField { id: inputtext
问题:我仍然可以查看文本项后面的星号。谁能帮我解决这个问题。非常有趣的问题。但我认为用这种方式覆盖
TextField
的可见文本是不可能的。至于解决方案,我只能提供这个蹩脚的解决方案:
TextField {
id: inputtext
anchors.centerIn: parent
echoMode:TextInput.Password
horizontalAlignment: TextInput.Center
onEditingFinished: mask.visible = false
Rectangle {
id: mask
anchors.fill: parent
color: "white"
border.width: 1
border.color: "#999"
Text {
anchors.centerIn: parent
font: inputtext.font
text: new Array(inputtext.text.length).join( "*" ) + inputtext.text.charAt(inputtext.text.length - 1)
}
}
}
我只是做了很少的测试。但是,它应该给你一个想法
TextField {
id: pwdField
property string password: ""
// could be ugly solution, but does the job
property string replaceString: "*******************************************************************"
property bool ignoreNext: false
placeholderText: "some text"
onTextChanged: {
if (ignoreNext) return;
var txtLength = text.length;
if (txtLength === 0) {
password = text;
updateTimer.stop();
} else {
if (txtLength < password.length) {
password = password.substring(0, txtLength);
console.log(text, "pwd: ", password)
return;
} else if (txtLength > password.length) {
password += text.charAt(txtLength - 1)
}
if (updateTimer.running) {
obscureText();
ignoreNext = true
text = text.substring(0, txtLength - 1) + password.charAt(password.length - 1);
ignoreNext = false
}
updateTimer.restart();
}
console.log(text, "pwd: ", password)
}
onEditingFinished: {
updateTimer.stop();
obscureText();
}
function obscureText() {
ignoreNext = true;
text = replaceString.substring(0, text.length);
ignoreNext = false;
}
Timer {
id: updateTimer
running: false
repeat: false
interval: 1000
onTriggered: pwdField.obscureText();
}
}
TextField{
id:pwdField
属性字符串密码:“”
//这可能是一个丑陋的解决方案,但确实有效
属性字符串替换字符串:******************************************************************************************************************
属性bool ignoreNext:false
占位符文本:“一些文本”
ContextChanged:{
如果(忽略下一步)返回;
var txtLength=text.length;
如果(txtLength==0){
密码=文本;
updateTimer.stop();
}否则{
if(txtLengthpassword.length){
密码+=text.charAt(txtLength-1)
}
if(updateTimer.running){
模糊文本();
ignoreNext=true
text=text.substring(0,txtLength-1)+password.charAt(password.length-1);
ignoreNext=false
}
restart();
}
日志(文本,“pwd:”,密码)
}
onEditingFinished:{
updateTimer.stop();
模糊文本();
}
函数obsolizeText(){
ignoreNext=true;
text=replaceString.substring(0,text.length);
ignoreNext=false;
}
计时器{
id:updateTimer
跑步:错
重复:错
间隔时间:1000
onTriggered:pwdField.obsolizeText();
}
}
您必须解决的问题:
TextField {
id: inputtext
anchors.centerIn: parent
echoMode:TextInput.Password
horizontalAlignment: TextInput.Center
onEditingFinished: mask.visible = false
Rectangle {
id: mask
anchors.fill: parent
color: "white"
border.width: 1
border.color: "#999"
Text {
anchors.centerIn: parent
font: inputtext.font
text: new Array(inputtext.text.length).join( "*" ) + inputtext.text.charAt(inputtext.text.length - 1)
}
}
}
- 您不能再真正使用
属性,因为它将为您提供所有文本
、使用****
和密码
信号onPasswordChanged
- 如果用户使用导航键移动光标,此解决方案将不起作用
TextInput
而不是TextField
,因为它是qml
中的基本元素。让我们以下面的密码输入为例,我使用TextInput
的passwordMaskDelay
属性,该属性是在Qt
5.4中引入的
Rectangle {
id: container
width: 200
height: 200
color: "yellow"
TextInput {
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
id: text2
echoMode: TextInput.Normal
font.pixelSize: 20; font.bold: true
text: "Enter text here..."
width: container.width - 40 ;
focus: true
passwordMaskDelay: 1000
}
MouseArea {
anchors.fill: parent
onClicked: {
text2.text = "";
text2.echoMode = TextInput.Password;
}
}
}
我想你想要一个透明的背景,我已经记住了。您可以尝试一下。谢谢您的回复。但我觉得有些不好,我还是遵循同样的逻辑。可以检查更新的代码我认为这不是一个好的解决方案
TextInput
已经有一个名为passwordMaskDelay
的属性来处理此类问题。看看我的解决方案。
Rectangle {
id: container
width: 200
height: 200
color: "yellow"
TextInput {
anchors.verticalCenter: parent.verticalCenter
anchors.horizontalCenter: parent.horizontalCenter
id: text2
echoMode: TextInput.Normal
font.pixelSize: 20; font.bold: true
text: "Enter text here..."
width: container.width - 40 ;
focus: true
passwordMaskDelay: 1000
}
MouseArea {
anchors.fill: parent
onClicked: {
text2.text = "";
text2.echoMode = TextInput.Password;
}
}
}