Qt 汉字限制个数

yuzoubawang88 2013-02-26 08:52:52
我想在在lineEdit里面判断我输入进去的汉字个数,比如:“我爱中国人”,我只要取里面的"我爱中国",这个要怎么做呢。。?就是我汉字最多只要四个汉字,但是如果是英文或数字,我最多要八个。。求各位大神赐教啊。。。。
...全文
332 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zheguzai000 2013-02-26
  • 打赏
  • 举报
回复
这个很简单的,比如你的文本框中输入的文字为“我爱中国人”,你只向要“我爱中国”这几个字
用ui->lineEdit->text().left(4);就可以拿到文本框中左边的四个字;
用ui->lineEdit->text().count();可以统计当前文本框中的字符串长度;





void Dialog::on_pushButton_clicked()
{
QString str;

str = ui->lineEdit->text().left(4);
qDebug()<< ui->lineEdit->text().count();
QMessageBox::information(this,tr("获取"),str);
}
醉过方知酒烈 2013-02-26
  • 打赏
  • 举报
回复
最后少了两句: return strRtn; }
醉过方知酒烈 2013-02-26
  • 打赏
  • 举报
回复 1
自己写一个: QString HandStringLen( const QString & _str, int _len )//_len就是你说的4个 { QString strRtn; int intLettercount = 0, intCharacterCount = 0, intStrLen = 0; if( _len < 0 || _str.isEmpty() ) return _str; strRtn = _str; if( strRtn.size() > 0 ) { intLettercount = 0; intCharacterCount = 0; for( int i = 0; i < strRtn.size(); i++ ) { if( strRtn.at(i).isLetter()) intLettercount ++; else intCharacterCount ++; intStrLen = intLettercount*2 + intCharacterCount; if( intStrLen >= _len*2 ) { strRtn = strRtn.left(intCharacterCount+intLettercount); break; } } }
乔巴好萌 2013-02-26
  • 打赏
  • 举报
回复
继承QLineEdit 重载下面的方法textEdited 见说明 void QLineEdit::textEdited ( const QString & text ) [signal] This signal is emitted whenever the text is edited. The text argument is the next text. Unlike textChanged(), this signal is not emitted when the text is changed programmatically, for example, by calling setText(). 当每次用户输入时,触发该事件,text即为输入 如果长度不超过界限 则调用setText设置输入 否则不输入
我是学友 2013-02-26
  • 打赏
  • 举报
回复
引用 4 楼 zheguzai000 的回复:
这个很简单的,比如你的文本框中输入的文字为“我爱中国人”,你只向要“我爱中国”这几个字 用ui->lineEdit->text().left(4);就可以拿到文本框中左边的四个字; 用ui->lineEdit->text().count();可以统计当前文本框中的字符串长度; C/C++ code?12345678void Dialog::on_pu……
这么做很不严谨,字符编码可不是一个汉字就占一个位置
tt2com 2013-02-26
  • 打赏
  • 举报
回复
首先需要确定你的编码使用 如果在使用windows的编码那么汉字的编码长度为2,你可以直接利用count()来进行统计 如果使用unicode的编码,那么你需要自己统计输入的为中文还是英/数字符来进行限制

16,235

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧