赞
踩
开发路上已经有两年了,才开始关注CSDN文章编写,每次在CSDN上看到别人分享技术的时候,觉得这个意义真的很大,今天尝试把自己的技术分享一下,虽然可能真的不成熟,技术也还是个小白水平,但做事总是要有意义的,分享就是这个意义!
这是一个简单的演示。
整个程序主要分为UI设计和拼音处理两个模块:
拼音(pinyin.pri): 该模块主要使用Google拼音的函数进行对字库的获取,主要是需要他提供的函数,结合QT C++的编写,实现输出文字。
UI设计(ui): UI就直接使用QT强大的ui设计功能,使用QPushButton、QLabel等等实现,设计的UI界面也是相当简单呀,哈哈哈。
测试界面(Test): 程序中我只是做个简单的测试,目前输入法针对能输入的编辑框内,都可以进行输入,另外有一些复杂的场景,使用效果可能达不到想要的效果。
标题就说做的一个简易输入法,相比强大的输入法,这边只是个小开发
拼音处理:
拼音获取中文文本-代码片段:
这是个演示
// 查找符合的候选数 size_t CWordsDlg::searchCandidate(QString spell_buf, int spell_len) { size_t len = static_cast<size_t>(spell_len); size_t cand_num = im_search(spell_buf.toUtf8().data(), len); if (cand_num < static_cast<size_t>(MAX_CANDIDATE_LEN)) { return cand_num; } return static_cast<size_t>(MAX_CANDIDATE_LEN); } // 获取候选字列表 QStringList CWordsDlg::getCanidateList(size_t cand_num) { if (cand_num == 0) { return QStringList(""); } QStringList cand_list; cand_list.clear(); char16 *cand_buf = new char16[MAX_CANDIDATE_LEN]; for (size_t i = 0; i < cand_num; i++) { char16 *cand; cand = im_get_candidate(i, cand_buf, MAX_CANDIDATE_LEN); if (cand != NULL) { cand_list.append(QString::fromUtf16(cand)); } else { continue; } } delete[] cand_buf; return cand_list; } // 键盘通过1 2 3..选词 void CWordsDlg::slotChooseWords(const int key) { int listWordsCount = ui->listWidget_words->count(); if (listWordsCount >= 0 && listWordsCount >= key) { QString words = ui->listWidget_words->item(key - 1)->text(); emit sigSendWords(words); ui->label_py->clear(); ui->listWidget_words->clear(); this->hide(); } else { return; } }
键盘UI-代码片段:
UI的设计不重要,怎么样还是看个人
遍历绑定键盘按钮信号和槽,以及部分按钮功能需求设置↓
QList<QPushButton *> btns = this->findChildren<QPushButton *>();
for (QList<QPushButton *>::iterator it = btns.begin(); it != btns.end(); it++)
{
QPushButton *tbtn = static_cast<QPushButton *>(*it);
QString objName = tbtn->objectName();
if (objName == "btn_Back")
{
tbtn->setAutoRepeat(true);
tbtn->setAutoRepeatDelay(200);
tbtn->setAutoRepeatInterval(100);
}
connect(tbtn, &QPushButton::pressed, this, &CInputKeyboard::slotPressBtn);
}
键盘位置,防遮挡设计↓
void CInputKeyboard::setKeyBoardRect(const QPoint formPos) { QPoint movePos; if ((m_desktopWidth - formPos.x()) > m_formWidth && (m_desktopHeight - formPos.y()) > m_formHeight*1.5) { movePos.setX(formPos.x()); movePos.setY(formPos.y() + m_formHeight/2); } else if((m_desktopWidth - formPos.x()) < m_formWidth && (m_desktopHeight - formPos.y()) > m_formHeight*1.5) { movePos.setX(m_desktopWidth - m_formWidth - 10); movePos.setY(formPos.y() + m_formHeight/2); } else if((m_desktopWidth - formPos.x()) > m_formWidth && (m_desktopHeight - formPos.y()) < m_formHeight*1.5) { movePos.setX(formPos.x() - m_formWidth/2); movePos.setY(formPos.y() - m_formHeight); } else if((m_desktopWidth - formPos.x()) < m_formWidth && (m_desktopHeight - formPos.y()) < m_formHeight*1.5) { movePos.setX(m_desktopWidth - m_formWidth - 10); movePos.setY(formPos.y() - m_formHeight); } this->move(movePos); m_wordsDlg->move(this->pos().x(), this->pos().y() - m_wordsDlg->height()); }
切换输入法状态,关联系统输入法状态↓
void CInputKeyboard::setLanguageStatus(languagetype type)
{
HWND hwnd = (HWND)m_focusWidget->winId();
HIMC hIMC = ImmGetContext(hwnd);
if (type == CH)
{
ImmSetOpenStatus(hIMC, true);
}
else if (type == EN)
{
ImmSetOpenStatus(hIMC, false);
}
}
代码总是可以优化,以上代码有什么可以优化的可以在后续优化……
上述仅仅是为了展示其部分,完整的程序可以下载参考、使用。
链接在这啦->简易中英文输入法下载
如果下载后想要使用本输入法到自己的项目上,请切记这不能商用,当然这输入法肯定不是第一选择,输入法功能单一,不适合商用,也不能;若仅仅是自己想要使用到自己的个人项目上,那完全可以。
使用到个人项目: 你可以将该输入法源码下的pinyin文件夹拷贝到你的项目文件夹下,后面调用即可
使用时在项目.Pro文件下插入
include($$PWD/pinyin/pinyin.pri); // $$PWD/pinyin/pinyin.pri为pinyin.pri文件的位置
使用时:直接在需要调用输入法页面(一般在构造函数内)
keyboard = new CInputKeyboard(this); //一定要给this,keyboard为调用页面的成员变量
因为本输入法使用的到了Win32API,所以可能导致输入法下载后仅能在Windows系统上运行,若要跨平台,个人可在对程序理解后进行二次开发。
注意: 本人之前在开发时,使用的是QT MSVC2017 32位,代码有部分也是用到了Win32API,在下载后编译时注意环境是否搭好,不清楚的可以私信,看到回复……
结尾:
以上就是本次的分享啦,有什么不好的还望看到的大佬指点,技术差,勿喷
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。