QlineEdit输入字符奇怪自动补全上一次字符而且交叉影响
做一个名称校验的函数,不能输入特殊字符;
1 | SlotTextChanged函数作用是判断是否包含特殊字符,有的话,弹出提示,删除特殊字符之后,在设置回去;发现输入/之后,弹出模态提示,自动删除后,在右侧车牌号码输入框中输入任意字符a会自动变成/a;是模态对话框打断了变化消息,当在右侧输入字符时,触发了消息,会把上次输入的消息在复现出来; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | void EmptyTreeItem::SlotTextChanged(QString strText) { if (strText.size()>250) { WaringDialog diag( "提示" , "输入名称太长,请限制250个字符!" ); diag.setPicType(-2); diag.exec(); strText = strText.left(250); ui.lineEditName->setText(strText); } if (strText.contains( "\\" )|| strText.contains( "/" )|| strText.contains( ":" )|| strText.contains( "*" ) || strText.contains( "?" )|| strText.contains( "\"" )||strText.contains( "<" ) || strText.contains( ">" ) || strText.contains( "|" )) { WaringDialog diag( "提示" , "不能包含特殊字符:\\ : / * ? \" < > |" ); diag.setPicType(-2); diag.exec(); strText=strText.replace( "\\" , "" ); strText = strText.replace( "/" , "" ); strText = strText.replace( ":" , "" ); strText = strText.replace( "*" , "" ); strText = strText.replace( "?" , "" ); strText = strText.replace( "\"" , "" ); strText = strText.replace( "<" , "" ); strText = strText.replace( ">" , "" ); strText = strText.replace( "|" , "" ); ui.lineEditName->setText(strText); } } |
发现是模态窗口WaringDialog 导致,所以解决办法,就是用信号槽的方式,SlotTextChanged函数发信号,把模态提示放在变化之后弹出,避免阻塞输入变化;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | void EmptyTreeItem::SlotTextChanged(QString strText) { if (strText.size()>100) { strText = strText.left(100); ui.lineEditName->setText(strText); emit signalError(-2, "文字长度不能超过100" ); } if (strText.contains( "\\" )|| strText.contains( "/" )|| strText.contains( ":" )|| strText.contains( "*" ) || strText.contains( "?" )|| strText.contains( "\"" )||strText.contains( "<" ) || strText.contains( ">" ) || strText.contains( "|" )) { if (strText.contains( "\\" )) { strText = strText.remove( "\\" ); } if (strText.contains( "/" )) { strText = strText.remove( "/" ); } if (strText.contains( ":" )) { strText = strText.remove( ":" ); } if (strText.contains( "*" )) { strText = strText.remove( "*" ); } if (strText.contains( "?" )) { strText = strText.remove( "?" ); } if (strText.contains( "\"" )) { strText = strText.remove( "\"" ); } if (strText.contains( "<" )) { strText = strText.remove( "<" ); } if (strText.contains( ">" )) { strText = strText.remove( ">" ); } if (strText.contains( "|" )) { strText = strText.remove( "|" ); } ui.lineEditName->setText(strText); emit signalError(-2, "不能包含特殊字符:\\ : / * ? \" < > |" ); } } |
信号槽连接
connect(this, &EmptyTreeItem::signalError, this, &EmptyTreeItem::SlotError, Qt::QueuedConnection); }
定义
1 2 3 4 5 | public slots: void SlotTextChanged(QString strText); void SlotError( int type, QString strContent); signals: void signalError( int type, QString strContent); |
自己开发了一个股票智能分析软件,功能很强大,需要的关注微信公众号:QStockView
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix