随笔 - 70, 文章 - 0, 评论 - 9, 引用 - 0
数据加载中……

Qt中根据拼音搜索汉字的问题

解决思路:
1  在数据库中增加一个“汉字--拼音”的数据表;
2  在QLineEdit输入框中监听到textChanged时,对于输入的字母进行判断处理:
    1)根据最后一个字母搜索到匹配的汉字;
    2)如果这个是第一个字母,那么根据这些汉字去查询数据结果,如果查询到,则计入内存(Hash表)中;如果不是第一个字母,就要根据前面的内存表再连接这个汉字列表来查询数据结果,如果查询到结果大于0个,则计入内存中。
3  对于内存中已存在的结果,直接匹配数据库,直接查询数据库结果。

注意:如果遇到字符格式问题,请使用toLocal8Bit()方法。


附上根据汉字获取首字母的代码:(参考:http://topic.csdn.net/u/20110627/15/dbb95eda-386d-4309-a482-d56955a5d6cd.html
QString PinYinHelper::getChineseSpell(QString& src)
{
 unsigned 
char ucHigh, ucLow;
 
int  nCode;
 QString strPinYin;
 
for (int i=0; i<src.length(); i++)
 {
  ucHigh 
= src[i].unicode();
  
if (ucHigh < 0x80 )//英文字母
  {
   strPinYin.append(src[i]);
   
continue;
  }
  ucLow  
= src[i + 1].unicode();
  
if ( ucHigh < 0xa1 || ucLow < 0xa1)
  {
   
continue;
  }
  
else
  {
   nCode 
= (ucHigh - 0xa0* 100 + ucLow - 0xa0;
  }
  strPinYin.append(FirstLetter(nCode));
  i
++;
 }
 
return strPinYin;
}
QString PinYinHelper::FirstLetter(
int nCode)
{
 
if(nCode >= 1601 && nCode < 1637return "A";
 
if(nCode >= 1637 && nCode < 1833return "B";
 
if(nCode >= 1833 && nCode < 2078return "C";
 
if(nCode >= 2078 && nCode < 2274return "D";
 
if(nCode >= 2274 && nCode < 2302return "E";
 
if(nCode >= 2302 && nCode < 2433return "F";
 
if(nCode >= 2433 && nCode < 2594return "G";
 
if(nCode >= 2594 && nCode < 2787return "H";
 
if(nCode >= 2787 && nCode < 3106return "J";
 
if(nCode >= 3106 && nCode < 3212return "K";
 
if(nCode >= 3212 && nCode < 3472return "L";
 
if(nCode >= 3472 && nCode < 3635return "M";
 
if(nCode >= 3635 && nCode < 3722return "N";
 
if(nCode >= 3722 && nCode < 3730return "O";
 
if(nCode >= 3730 && nCode < 3858return "P";
 
if(nCode >= 3858 && nCode < 4027return "Q";
 
if(nCode >= 4027 && nCode < 4086return "R";
 
if(nCode >= 4086 && nCode < 4390return "S";
 
if(nCode >= 4390 && nCode < 4558return "T";
 
if(nCode >= 4558 && nCode < 4684return "W";
 
if(nCode >= 4684 && nCode < 4925return "X";
 
if(nCode >= 4925 && nCode < 5249return "Y";
 
if(nCode >= 5249 && nCode < 5590return "Z";
 
return "";
}

posted on 2012-04-09 09:49 seahouse 阅读(2819) 评论(0)  编辑 收藏 引用 所属分类: Qt


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理