找回密码
 立即注册
收起左侧

Qt编写输入法V2019终极版

0
回复
4384
查看
[复制链接]
累计签到:7 天
连续签到:1 天
来源: 2019-8-15 16:01:23 显示全部楼层 |阅读模式

马上注册,查看详细内容!注册请先查看:注册须知

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
一、前言
之前写过的V2018版本的输入法,本来已经很完善了,不打算更新升级了,最近有个朋友找我定制一个输入法,需要高仿一个苹果MAC电脑的输入法,MAC操作系统的审美无疑是相当棒的,于是乎直接拿以前的输入法高仿了一个,由于之前有做过输入法这块的开发,而且改进了四年,各种需求都遇到过,陆陆续续完善了很多年,所以这个高仿起来难度不大,而且要支持滑动选词,直接撸代码。
体验地址:https://pan.baidu.com/s/1vIyEdB4QGo5OvxLYj7kq5g   提取码:sysn


二、功能特点
1. 未采用Qt系统层输入法框架,独创输入切换机制。
2. 纯QWidget编写,支持任何目标平台(亲测windows、linux、嵌入式linux等),支持任意Qt版本(亲测Qt4.6.0到Qt5.13),支持任意编译器(亲测mingw、gcc、msvc等),支持任意控件输入包括网页中的输入控件。
3. 调用极为方便,pri文件调用形式,只要改成文件包含即可,例如pro文件中写 include($$PWD/input2019/input2019.pri)。
4. 界面清晰简洁,UI美观友好,高仿IOS输入法,非常适合触摸设备。
5. 顶部滑动选词+弹出汉字面板选词,支持滑动。
6. 具有记忆功能,之前选中过的词语首先显示,支持单个拼音多个汉字,自动调整优先级。
7. 具有造词功能,可以直接打开文件文件写入自定义词组,最高级别显示。
8. 支持Qt程序嵌入的浏览器中的网页中的文本框等控件的输入。
9. 界面大小随意设置,采用布局自使用任何分辨率。
10. 属性控制数字输入,例如需要文本框默认弹出的是数字则设置代码 ui->txt->setProperty("flag", "number");
11. 自由控制需要显示输入法和不需要显示输入法,当某些控件不需要弹出输入法,只需要对应不需要弹出输入法的控件设置属性noinput为真即可。例如ui->txt->setProperty("noinput", true);
12. 界面自适应屏幕大小,输入法弹出位置为控件底部时,当超过桌面右边或者底部时,自动调整位置。
13. 实现了长按超过500毫秒重复执行按下的键的功能。例如长按退格键,不断删除。
14. 英文、中文、数字字母、大小写、特殊字符自由切换。
15. 支持单拼、全拼、模糊拼音输入,智能分页算法,可任意翻页查看汉字词组。
16. 默认自带5种皮肤颜色,可随意切换,用户也可用QSS自定义皮肤。
17. 谷歌内核的输入法引擎,品质保证,字库文件1MB,不依赖数据库,资源占用低效率极高。支持模糊拼音,比如nh=你好。
18. 可选windows专有版本,支持外部程序输入,比如输入到记事本、QQ聊天窗口等。
19. 整个输入法代码行数1000行左右,非常小,不会对程序增加大小造成负担。
20. 代码结构极为清晰,注释详细,非常容易阅读和理解,同时也可以自行修改拓展自定义的需求。

三、效果图




四、使用方法
1. 将input2019整个目录放到你的项目的pro同一级别目录中。
2. 在你的主程序的pro文件中加一行 include($$PWD/input2019/input2019.pri)
3. 在你的程序的main函数中引入头文件 #include "input2019/frminput2019.h"
4. 在你的程序的main函数中加一行代码 QApplication a(argc, argv);之后加 frmInput2019::Instance()->hide();
5. 将源码下的dict_pinyin.dat+dict_pinyin_user.dat字库文件复制到可执行文件同一目录。


五、其他说明
1. 如果想设置更小的尺寸,可用setFixedSize。
2. 源码下的chinese_user.txt为自定义词组文件,打开编辑即可,该文件放到可执行文件同一目录即可。
3. 如果是dialog窗体,请在dialog窗体exec前增加一行代码,QDialog dialog;dialog.setWindowModality(Qt::WindowModal);否则会阻塞窗体消息。
4. 在某些嵌入式linux系统中,如果没有带有XCB,则输入法需要先show再hide一次,然后输入法才能起作用,不然程序会崩溃。

六、核心代码
  1. bool frmInput2019::eventFilter(QObject *watched, QEvent *event)
  2. {
  3.     if (watched == this) {
  4.         //处理自身拖动
  5.         static QPoint mousePoint;
  6.         static bool mousePressed = false;
  7.         QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);

  8.         //按下的时候记住坐标,移动到鼠标松开的位置
  9.         if (event->type() == QEvent::MouseButtonPress) {
  10.             if (mouseEvent->button() == Qt::LeftButton) {
  11.                 mousePressed = true;
  12.                 mousePoint = mouseEvent->globalPos() - this->pos();
  13.                 return true;
  14.             }
  15.         } else if (event->type() == QEvent::MouseButtonRelease) {
  16.             mousePressed = false;
  17.             return true;
  18.         } else if (event->type() == QEvent::MouseMove) {
  19.             if (mousePressed && (mouseEvent->buttons() && Qt::LeftButton && position != "bottom")) {
  20.                 this->move(mouseEvent->globalPos() - mousePoint);
  21.                 this->update();
  22.                 return true;
  23.             }
  24.         }
  25.     } else if (watched == ui->labMore) {
  26.         if (event->type() == QEvent::MouseButtonPress) {
  27.             if (inputType == "chinese" && !upper && labCn.first()->isEnabled()) {
  28.                 if (!ui->widgetChinese->isVisible()) {
  29.                     ui->widgetLetter->setVisible(false);
  30.                     ui->widgetNumber->setVisible(false);
  31.                     ui->widgetChinese->setVisible(true);
  32.                 } else {
  33.                     ui->widgetLetter->setVisible(true);
  34.                     ui->widgetNumber->setVisible(false);
  35.                     ui->widgetChinese->setVisible(false);
  36.                 }

  37.                 //重新设置图标
  38.                 QString strMore = ui->widgetMore->isVisible() ? "up" : "down";
  39.                 ui->labMore->setPixmap(QString(":/image/btn_%1_%2.png").arg(strMore).arg(iconType));
  40.                 return true;
  41.             }
  42.         }
  43.     } else if (watched == ui->labType) {
  44.         if (event->type() == QEvent::MouseButtonPress) {
  45.             if (inputType == "english") {
  46.                 setInputType("chinese");
  47.             } else if (inputType == "chinese") {
  48.                 setInputType("english");
  49.             }
  50.         }
  51.     } else if (watched == ui->labType2) {
  52.         if (event->type() == QEvent::MouseButtonPress) {
  53.             setInputType("english");
  54.         }
  55.     } else if (watched == ui->widgetCn) {
  56.         //没有汉字或者按下的地方没有汉字或者当前汉字标签个数过少都不用继续
  57.         if (!labCn.first()->isEnabled() || lastText.isEmpty()) {
  58.             return false;
  59.         }

  60.         //记住最后按下拖动的时间,过短则认为是滑动,启动滑动动画
  61.         static bool pressed = false;
  62.         static QPoint lastPos = QPoint();
  63.         static QDateTime lastTime = QDateTime::currentDateTime();
  64.         QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);

  65.         if (event->type() == QEvent::MouseButtonPress) {
  66.             pressed = true;
  67.             lastPos = mouseEvent->pos();
  68.             animationCn->stop();
  69.             lastTime = QDateTime::currentDateTime();
  70.         } else if (event->type() == QEvent::MouseButtonRelease) {
  71.             pressed = false;
  72.             if (lastPos != mouseEvent->pos()) {
  73.                 //判断当前时间和鼠标按下事件比较,时间短则说明是滑动
  74.                 QDateTime now = QDateTime::currentDateTime();
  75.                 if (lastTime.msecsTo(now) < 600) {
  76.                     //可以改变下面的值来调整幅度
  77.                     bool moveleft = (mouseEvent->pos().x() - lastPos.x()) < 0;
  78.                     int offset = moveleft ? 350 : -350;
  79.                     int value = ui->scrollAreaCn->horizontalScrollBar()->value();
  80.                     animationCn->setStartValue(value);
  81.                     animationCn->setEndValue(value + offset);
  82.                     animationCn->start();
  83.                 }
  84.             }
  85.         } else if (event->type() == QEvent::MouseMove) {
  86.             if (pressed && labCn.first()->isEnabled()) {
  87.                 //计算滑过的距离
  88.                 bool moveleft = (mouseEvent->pos().x() - lastPos.x()) < 0;
  89.                 int offset = moveleft ? 5 : -5;
  90.                 int value = ui->scrollAreaCn->horizontalScrollBar()->value();
  91.                 ui->scrollAreaCn->horizontalScrollBar()->setValue(value + offset);
  92.                 return true;
  93.             }
  94.         }
  95.     } else if (watched == ui->widgetMore) {
  96.         //没有汉字或者按下的地方没有汉字或者当前汉字标签个数过少都不用继续
  97.         if (!labMore.first()->isEnabled() || lastText.isEmpty()) {
  98.             return false;
  99.         }

  100.         //记住最后按下拖动的时间,过短则认为是滑动,启动滑动动画
  101.         static bool pressed = false;
  102.         static QPoint lastPos = QPoint();
  103.         static QDateTime lastTime = QDateTime::currentDateTime();
  104.         QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);

  105.         if (event->type() == QEvent::MouseButtonPress) {
  106.             pressed = true;
  107.             lastPos = mouseEvent->pos();
  108.             animationMore->stop();
  109.             lastTime = QDateTime::currentDateTime();
  110.         } else if (event->type() == QEvent::MouseButtonRelease) {
  111.             pressed = false;
  112.             if (lastPos != mouseEvent->pos()) {
  113.                 //判断当前时间和鼠标按下事件比较,时间短则说明是滑动
  114.                 QDateTime now = QDateTime::currentDateTime();
  115.                 if (lastTime.msecsTo(now) < 600) {
  116.                     //可以改变下面的值来调整幅度
  117.                     bool movebottom = (mouseEvent->pos().y() - lastPos.y()) < 0;
  118.                     int offset = movebottom ? 150 : -150;
  119.                     int value = ui->scrollAreaMore->verticalScrollBar()->value();
  120.                     animationMore->setStartValue(value);
  121.                     animationMore->setEndValue(value + offset);
  122.                     animationMore->start();
  123.                 }
  124.             }
  125.         } else if (event->type() == QEvent::MouseMove) {
  126.             if (pressed && labMore.first()->isEnabled()) {
  127.                 //计算滑过的距离
  128.                 bool movebottom = (mouseEvent->pos().y() - lastPos.y()) < 0;
  129.                 int offset = movebottom ? 5 : -5;
  130.                 int value = ui->scrollAreaMore->verticalScrollBar()->value();
  131.                 ui->scrollAreaMore->verticalScrollBar()->setValue(value + offset);
  132.                 return true;
  133.             }
  134.         }
  135.     } else if (watched->inherits("QLabel")) {
  136.         QLabel *lab = (QLabel *)watched;
  137.         if (!upper && inputType == "chinese") {
  138.             if (lab->property("labCn").toBool()) {
  139.                 //记住最后按下的滚动条位置,如果滚动条一直没有变化则认为单击了标签
  140.                 static int lastPosition = 0;
  141.                 if (event->type() == QEvent::MouseButtonPress) {
  142.                     lastPosition = ui->scrollAreaCn->horizontalScrollBar()->value();
  143.                     lastText = lab->text();
  144.                 } else if (event->type() == QEvent::MouseButtonRelease) {
  145.                     if (lastPosition == ui->scrollAreaCn->horizontalScrollBar()->value() && !lastText.isEmpty()) {
  146.                         insertValue(lab->text());
  147.                         clearChinese();
  148.                     }
  149.                 }
  150.             } else if (lab->property("labMore").toBool()) {
  151.                 //记住最后按下的滚动条位置,如果滚动条一直没有变化则认为单击了标签
  152.                 static int lastPosition = 0;
  153.                 if (event->type() == QEvent::MouseButtonPress) {
  154.                     lastPosition = ui->scrollAreaMore->verticalScrollBar()->value();
  155.                     lastText = lab->text();
  156.                 } else if (event->type() == QEvent::MouseButtonRelease) {
  157.                     if (lastPosition == ui->scrollAreaMore->verticalScrollBar()->value() && !lastText.isEmpty()) {
  158.                         insertValue(lab->text());
  159.                         clearChinese();
  160.                     }
  161.                 }
  162.             }
  163.         }
  164.     } else {
  165.         if (event->type() == QEvent::MouseButtonPress) {
  166.             if (currentWidget != 0) {
  167.                 if (!isVisible()) {
  168.                     showPanel();
  169.                 }
  170.             } else {
  171.                 if (isVisible()) {
  172.                     hidePanel();
  173.                 }
  174.             }
  175.         }
  176.     }

  177.     return QWidget::eventFilter(watched, event);
  178. }

  179. void frmInput2019::initForm()
  180. {
  181. #if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
  182.     setWindowFlags(Qt::Tool | Qt::WindowDoesNotAcceptFocus | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint);
  183. #else
  184.     setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint);
  185. #endif

  186.     currentWidget = 0;
  187.     upper = false;
  188.     number = false;
  189.     onlyControl = false;
  190.     autoHide = false;
  191.     columnCount = 8;
  192.     maxCount = 256;
  193.     dbPath = qApp->applicationDirPath();

  194.     //绑定按钮到事件
  195.     QList<QPushButton *> btns;
  196.     btns << ui->widgetLetter->findChildren<QPushButton *>();
  197.     btns << ui->widgetNumber->findChildren<QPushButton *>();
  198.     foreach (QPushButton *btn, btns) {
  199.         btn->setProperty("btnInput", true);
  200.         connect(btn, SIGNAL(clicked()), this, SLOT(btnClicked()));
  201.     }

  202.     //设置字母属性
  203.     btns.clear();
  204.     btns << ui->widgetLetter1->findChildren<QPushButton *>();
  205.     btns << ui->widgetLetter2->findChildren<QPushButton *>();
  206.     foreach (QPushButton *btn, btns) {
  207.         btn->setProperty("btnLetter", true);
  208.     }

  209.     //设置所有按钮输入法不可用+长按自动重复事件
  210.     btns.clear();
  211.     btns << this->findChildren<QPushButton *>();
  212.     foreach (QPushButton *btn, btns) {
  213.         btn->setFocusPolicy(Qt::NoFocus);
  214.         btn->setProperty("noinput", true);
  215.         btn->setAutoRepeat(true);
  216.         btn->setAutoRepeatDelay(500);
  217.     }

  218.     //默认最大生成256个,添加到顶部滚动区域中
  219.     for (int i = 0; i < maxCount; i++) {
  220.         QLabel *lab = new QLabel;
  221.         lab->setProperty("labCn", true);
  222.         lab->setEnabled(false);
  223.         ui->layout->addWidget(lab);
  224.         labCn << lab;
  225.     }

  226.     //默认最大生成256个,添加到更多滚动区域中
  227.     int row = 0;
  228.     int column = 0;
  229.     for (int i = 0; i < maxCount; i++) {
  230.         QLabel *lab = new QLabel;
  231.         lab->setProperty("labMore", true);
  232.         lab->setEnabled(false);
  233.         lab->setAlignment(Qt::AlignCenter);
  234.         lab->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
  235.         ui->gridLayout->addWidget(lab, row, column);
  236.         labMore << lab;

  237.         column++;
  238.         if (column >= columnCount) {
  239.             row++;
  240.             column = 0;
  241.         }
  242.     }

  243.     ui->lab1->setEnabled(false);
  244.     ui->lab2->setEnabled(false);
  245.     ui->labPY->setEnabled(false);
  246.     ui->labMore->setEnabled(false);

  247.     //输入法面板的字体名称和按钮字体大小即汉字区域字体大小
  248.     setFontInfo(this->font().family(), 11, 10);
  249.     //图标固定大小
  250.     setIconSize(20, 20);
  251.     //按钮之间的间隔
  252.     setSpacing(6);
  253.     //顶部汉字区域高度
  254.     setTopHeight(40);

  255.     //输入法面板的显示位置  control--显示在对应输入框的正下方 bottom--填充显示在底部  center--窗体居中显示
  256.     setPosition("control");
  257.     //输入法模式 english--英文模式  chinese--中文模式  number--数字特殊字符模式
  258.     setInputType("english");
  259.     //输入法面板的样式  black--黑色  blue--淡蓝色  brown--灰黑色  gray--灰色  silvery--银色
  260.     setStyleName("black");

  261.     //定义动画产生平滑数值
  262.     animationCn = new QPropertyAnimation(ui->scrollAreaCn->horizontalScrollBar(), "value");
  263.     animationCn->setEasingCurve(QEasingCurve::OutCirc);
  264.     animationCn->setDuration(500);

  265.     animationMore = new QPropertyAnimation(ui->scrollAreaMore->verticalScrollBar(), "value");
  266.     animationMore->setEasingCurve(QEasingCurve::OutCirc);
  267.     animationMore->setDuration(500);
  268. }

  269. void frmInput2019::init()
  270. {
  271.     if (onlyControl) {
  272.         ui->labPY->setVisible(false);
  273.         this->installEventFilter(this);
  274.         ui->labType->installEventFilter(this);
  275.         ui->labType2->installEventFilter(this);
  276.         ui->labMore->installEventFilter(this);
  277.         ui->widgetCn->installEventFilter(this);
  278.         ui->widgetMore->installEventFilter(this);

  279.         foreach (QLabel *lab, labCn) {
  280.             lab->installEventFilter(this);
  281.         }

  282.         foreach (QLabel *lab, labMore) {
  283.             lab->installEventFilter(this);
  284.         }
  285.     } else {
  286.         //绑定全局改变焦点信号槽
  287.         connect(qApp, SIGNAL(focusChanged(QWidget *, QWidget *)), this, SLOT(focusChanged(QWidget *, QWidget *)));
  288.         qApp->installEventFilter(this);
  289.     }

  290.     py.open(dbPath);
  291.     readChinese();
  292. }
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了