新版本:
| https://qtchina.blog.csdn.net/article/details/103945421 |
时间过得真快,不知不觉已到2015年,农历春节一眨眼就过去了,端正状态收拾心情整装待发出发。
曾经有段时间,我有一个很执着的梦想,我要导演出一部空前绝后的巨幕。不过现实无情地碾碎我的梦想,也同时将我推进了人生的低谷。
我开始思考,思想很多很多我以前总是回避的问题,在一次次思考中,我能感觉到我正在向一个陌生的地方走去,前途是光是暗也无法推测,我发现我在赌,赌注是一辈子的懊悔或是郁郁终生。
但当我看到阳光弥漫在我身边时,才发现以前的我是那么幼稚,我始终在找借口或不是借口的借口推卸掉与生俱来的责任,在光怪陆离的现实中找个僻静的地方希望过云淡风轻的日子。我错了,我拥有足够的能力却不敢恣意妄为,我拥有惊人的天赋却畏惧这只是南柯一梦,或者这本来就是梦,醒来时却是了无痕迹和无休止的眷恋。
从前有个同学问我是不是幻想过一些特别的经历会降临到自己身上,当时我只笑了笑,却不知道该怎么回答。
我想我是矛盾的,我认为我的存在与否关系到世间的万事万物,但我找不到足够的证据来证明我具有救世主的身份。因此我一度怀疑过我是不是生不逢时,或是天妒英才。
我在一次次轮回中等待着一个机会,也许是乱世,也许是太平,我没有所谓的一技之长,只有飞扬跋扈的骄傲和潮洗潮卷的胸怀。只有一笑置之的孤傲和白云苍狗的方式。我想我不该妄自菲薄,甚至这是理所当然的不加半点修饰的顺其自然。
我很喜欢这样一句话:如果我将庸碌一生的话,地球将停止转动,地球停止转动了吗?没有,那我将不会庸碌一生。
在特定的时间,特定的环境,特定的地点,有个特定的人将主宰万事万物!天生我才必有用!经典的心照不宣,只可惜他的傲慢却没有足够的能力做基垫。
我想我是对的,注定的便是永远无法改变的,过去的只是匆匆一瞥,将来的只是白驹过隙,一次次的轮回换来的是麻木的循规蹈矩,所以世界需要那么一个人----改变。
分割线-----------------------------------------------------------------------
高中和大学很喜欢文学,特别是在大学期间,在任何填写个人资料兴趣爱好一栏中填写的都是文学和编程,这就好比我的左手和右手,缺一不可。
好了言归正传,来说说Qt编写可换肤的中文拼音输入法。在ARM上,如果采用QT开发应用程序,基本上都会遇到一个问题,需要在应用程序中输入汉字,这个就比较麻烦,就算接了实体键盘,也只能输入字母和数字的,对中文无可奈何,为此就需要用QT自己来实现一个。
百度一搜索,有很多前辈都写过类似的输入法,但是基本上都是用QWSInputMethod相关的机制,我觉得这样有点不妥,绝大部分界面丑陋,只支持单个汉字输入,而且基本上不支持实体键盘和虚拟键盘同步输入。
运行截图:
特点:
1:各Qt版本(Qt4.6-Qt5.4)、各操作系统(windows、linux、嵌入式linux-ARM)全编译运行通过,界面一致。
2:支持小写、大写、中文三种模式输入,自由切换。
3:支持单拼双拼词组输入,网上大部分只支持单个汉字输入。
4:智能分页算法,可任意翻页查看汉字词组。
5:除了支持鼠标单击软键盘输入外,还支持实体键盘同步输入,代码中有映射处理,对应实体键盘的按键会反应到软键盘处理。
6:七套精美皮肤样式选择,鼠标右键任意换肤。
7:代码优化到极致,包括注释和所有完善性代码处理总共不到500行。
8:除了可以单击软键盘和实体键盘上的数字来选择对应的汉字外,还支持鼠标单击汉字显示处来选择汉字。
9:无边框窗体,可拖动移动。
10:sqlite数据库存储汉字字库,可自由添加自定义的词组。
11:参考实体键盘布局,更逼真。
12:界面布局、字库算法、界面切换100%首创处理,与任何网上的Qt输入法处理方式和布局截然不同。
核心方法:
1:获取当前焦点控件。
Qt封装了一个focusChanged信号,提供两个参数QWidget *oldWidget, QWidget *nowWidget,这样就给输入法的处理带来了极大的方便。
//绑定全局改变焦点信号槽
connect(qApp, SIGNAL(focusChanged(QWidget *, QWidget *)),this, SLOT(focusChanged(QWidget *, QWidget *)));
void frmInput::focusChanged(QWidget *oldWidget, QWidget *nowWidget)
{//qDebug() << "oldWidget:" << oldWidget << "nowWidget:" << nowWidget;if (nowWidget != 0 && !this->isAncestorOf(nowWidget)) {if (nowWidget->inherits("QLineEdit")) {currentLineEdit = (QLineEdit *)nowWidget;this->setVisible(true);} else {currentLineEdit = 0;this->setVisible(false);//需要将输入法切换到最初的原始状态--小写currentType="min";changeType(currentType);}}
}
2:小写、大写、中文三种模式切换。
void frmInput::changeType(QString type)
{if (type == "max") {changeLetter(true);ui->btnType->setText("大写");ui->widgetTop->setVisible(false);this->setGeometry(this->geometry().x(), this->geometry().y(), 550, 160);} else if (type == "min") {changeLetter(false);ui->btnType->setText("小写");ui->widgetTop->setVisible(false);this->setGeometry(this->geometry().x(), this->geometry().y(), 550, 160);} else {changeLetter(false);ui->btnType->setText("中文");ui->widgetTop->setVisible(true);this->setGeometry(this->geometry().x(), this->geometry().y(), 550, 215);//每次切换到中文显示,都要执行清空之前中文模式下的信息clearChinese();ui->labPY->setText("");}
}
void frmInput::changeLetter(bool isUpper)
{QList<QPushButton *> btn = this->findChildren<QPushButton *>();foreach (QPushButton * b, btn) {if (b->property("btnLetter").toBool()) {if (isUpper) {b->setText(b->text().toUpper());} else {b->setText(b->text().toLower());}}}
}
3:软键盘按钮单击事件处理。
void frmInput::btn_clicked()
{QPushButton *btn = (QPushButton *)sender();QString objectName = btn->objectName();if (objectName == "btnType") {if (currentType == "min") {currentType = "max";} else if (currentType == "max") {currentType = "chinese";} else if (currentType == "chinese") {currentType = "min";}changeType(currentType);} else if (objectName == "btnDelete" || objectName == "btnDeleteMin") {//如果当前是中文模式,则删除对应拼音,删除完拼音之后再删除对应文本输入框的内容if (currentType == "chinese") {QString txt = ui->labPY->text();int len = txt.length();if (len > 0) {ui->labPY->setText(txt.left(len - 1));selectChinese();} else {if (currentLineEdit != 0) {currentLineEdit->backspace();}}} else {if (currentLineEdit != 0) {currentLineEdit->backspace();}}} else if (objectName == "btnPre") {if (currentPY_index >= 20) {if (currentPY_index % 10 == 0) {currentPY_index -= 20;} else {currentPY_index = currentPY_count - (currentPY_count % 10) - 10;}} else {currentPY_index = 0;}showChinese();} else if (objectName == "btnNext") {if (currentPY_index < currentPY_count - 1) {showChinese();}} else {QString value = btn->text();//如果是&按钮,因为对应&被过滤,所以真实的text为去除前面一个&字符if (objectName == "btnOther7") {value = value.right(1);}//当前不是中文模式,则单击按钮对应text为传递参数if (currentType != "chinese") {if (currentLineEdit != 0) {currentLineEdit->insert(value);}} else {if (currentLineEdit != 0) {//中文模式下,不允许输入特殊字符,单击对应数字按键取得当前索引的汉字if (btn->property("btnOther").toBool()) {if (ui->labPY->text().length() == 0) {currentLineEdit->insert(value);}} else if (btn->property("btnNum").toBool()) {if (ui->labPY->text().length() == 0) {currentLineEdit->insert(value);} else if (objectName == "btn0") {setChinese(0);} else if (objectName == "btn1") {setChinese(1);} else if (objectName == "btn2") {setChinese(2);} else if (objectName == "btn3") {setChinese(3);} else if (objectName == "btn4") {setChinese(4);} else if (objectName == "btn5") {setChinese(5);} else if (objectName == "btn6") {setChinese(6);} else if (objectName == "btn7") {setChinese(7);} else if (objectName == "btn8") {setChinese(8);} else if (objectName == "btn9") {setChinese(9);}} else if (btn->property("btnLetter").toBool()) {ui->labPY->setText(ui->labPY->text() + value);selectChinese();}}}}
}
4:汉字列表单击选择。
//事件过滤器,用于识别鼠标单击汉字标签处获取对应汉字
bool frmInput::eventFilter(QObject *obj, QEvent *event)
{if (event->type() == QEvent::MouseButtonPress) {if (obj == ui->labCh0) {setChinese(0);} else if (obj == ui->labCh1) {setChinese(1);} else if (obj == ui->labCh2) {setChinese(2);} else if (obj == ui->labCh3) {setChinese(3);} else if (obj == ui->labCh4) {setChinese(4);} else if (obj == ui->labCh5) {setChinese(5);} else if (obj == ui->labCh6) {setChinese(6);} else if (obj == ui->labCh7) {setChinese(7);} else if (obj == ui->labCh8) {setChinese(8);} else if (obj == ui->labCh9) {setChinese(9);}return false;} else if (event->type() == QEvent::KeyPress) {//如果处于中文模式,需要将当前按键对应反应到输入法界面上if (currentType == "chinese") {QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);if (keyEvent->key() == Qt::Key_Backspace) {ui->btnDelete->click();} else {QString key = keyEvent->text();QList<QPushButton *> btn = this->findChildren<QPushButton *>();foreach (QPushButton * b, btn) {if (b->text() == key) {b->click();break;}}}return true;}return false;}return QObject::eventFilter(obj, event);
}
5:汉字智能分页算法。
void frmInput::selectChinese()
{clearChinese();QSqlQuery query;QString currentPY = ui->labPY->text();QString sql = "select [word] from [pinyin] where [pinyin]='" + currentPY + "';";query.exec(sql);//逐个将查询到的字词加入汉字队列while(query.next()) {QString result = query.value(0).toString();if (App::PYType == "danpin") {allPY.append(result);currentPY_count++;} else {QStringList text = result.split(" ");foreach (QString txt, text) {if (txt.length() > 0) {allPY.append(txt);currentPY_count++;}}}}showChinese();
}
void frmInput::showChinese()
{//每个版面最多显示10个汉字int count = 0;currentPY.clear();for (int i = 0; i < 10; i++) {labCh[i]->setText("");}for (int i = currentPY_index; i < currentPY_count; i++) {if (count == 10) {break;}QString txt = QString("%1.%2").arg(count).arg(allPY[currentPY_index]);currentPY.append(allPY[currentPY_index]);labCh[count]->setText(txt);count++;currentPY_index++;}//qDebug() << "currentPY_index:" << currentPY_index << "currentPY_count:" << currentPY_count;
}
















