做一个名称校验的函数,不能输入特殊字符;
SlotTextChanged函数作用是判断是否包含特殊字符,有的话,弹出提示,删除特殊字符之后,在设置回去;发现输入/之后,弹出模态提示,自动删除后,在右侧车牌号码输入框中输入任意字符a会自动变成/a;是模态对话框打断了变化消息,当在右侧输入字符时,触发了消息,会把上次输入的消息在复现出来;
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函数发信号,把模态提示放在变化之后弹出,避免阻塞输入变化;
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); }
定义
public slots: void SlotTextChanged(QString strText); void SlotError(int type, QString strContent); signals: void signalError(int type, QString strContent);
标签:字符,补全,diag,contains,remove,strText,QlineEdit,replace,输入 From: https://www.cnblogs.com/bclshuai/p/18083901