如题,项目中可能遇到此类问题,先写个马马虎虎的解决方法:
第一种方法,逐一取中QString的字符,进行判断。
- QString
str; - int
nCount = str.count(); - for(int
i = 0 ; i <</span> nCount ; i++) - {
-
QChar cha = str.at(i); -
ushort uni = cha.unicode(); -
if(uni >= 0x4E00 && uni <</span>= 0x9FA5) -
{ -
//这个字符是中文 -
} - }
第二种方法,是利用强大的正则表达式进行判断。
- QString
str; - bool
b = str.contains(QRegExp("[\\x4e00-\\x9fa5]+")); - if(b)
- {
-
//存在中文 - }
当然str这个对象,是要先有值了,这个值可以从QLineEdit,QStandItem等等常用的其对象来。
下面是从网上查找资料写的拼音码代码:
bool In(wchar_t start, wchar_t end, wchar_t code)
{
if (code >= start && code <= end)
{
return true;
}
return false;
}
char convert(int n)
{
if (In(0xB0A1,0xB0C4,n)) return 'a';
if (In(0XB0C5,0XB2C0,n)) return 'b';
if (In(0xB2C1,0xB4ED,n)) return 'c';
if (In(0xB4EE,0xB6E9,n)) return 'd';
if (In(0xB6EA,0xB7A1,n)) return 'e';
if (In(0xB7A2,0xB8c0,n)) return 'f';
if (In(0xB8C1,0xB9FD,n)) return 'g';
if (In(0xB9FE,0xBBF6,n)) return 'h';
if (In(0xBBF7,0xBFA5,n)) return 'j';
if (In(0xBFA6,0xC0AB,n)) return 'k';
if (In(0xC0AC,0xC2E7,n)) return 'l';
if (In(0xC2E8,0xC4C2,n)) return 'm';
if (In(0xC4C3,0xC5B5,n)) return 'n';
if (In(0xC5B6,0xC5BD,n)) return 'o';
if (In(0xC5BE,0xC6D9,n)) return 'p';
if (In(0xC6DA,0xC8BA,n)) return 'q';
if (In(0xC8BB,0xC8F5,n)) return 'r';
if (In(0xC8F6,0xCBF0,n)) return 's';
if (In(0xCBFA,0xCDD9,n)) return 't';
if (In(0xCDDA,0xCEF3,n)) return 'w';
if (In(0xCEF4,0xD188,n)) return 'x';
if (In(0xD1B9,0xD4D0,n)) return 'y';
if (In(0xD4D1,0xD7F9,n)) return 'z';
return '\0';
}
QString getChineseSpell(QString &src) //获得汉字拼音码
{
wchar_t wchr;
QString strPinYin;
for (int i=0; i<src.length(); i++)
{
QChar cha = src.at(i);
ushort uni = cha.unicode();
if(uni>=0X4E00 && uni<=0X9FA5) //汉字
{
QString str = src.at(i);
QByteArray arr = str.toLocal8Bit();
wchr = (arr.at(0) & 0xff) << 8;
wchr |= (arr.at(1) & 0xff);
strPinYin.append(convert(wchr));
}
else //英文
{
strPinYin.append(src[i]);
// continue;
}
}
return strPinYin;
}
void dqtx::on_lineEdit_textChanged(const QString &arg1)
{
QString strpy;
QString strpin;
strpin=arg1;
strpy = getChineseSpell(strpin);
qDebug()<<strpy;
qDebug()<<arg1;
}