Qt5中文输入的问题

一、前言

本博客是为了解决在Qt5.4和Qt5.5下的中文输入问题,博主所用的Qt版本是Qt5.5.1,中文输入法是搜狗输入法,操作系统是ubuntu 14.04

Qt5.3及以前的版本可以通过复制/usr/lib/x86_64-linux-gnu/qt5/plugins/platforminputcontexts/下的libfcitxplatforminputcontextplugin.soQt5.3.1/5.3/gcc_64/plugins/platforminputcontexts
/Qt5.3.1/Tools/QtCreator/bin/plugins/platforminputcontexts 解决Qt下的中文输入问题,但是Qt5.4和Qt5.5,旧办法失效,原因是Qt5.4后对之前Qt5版本不再二进制兼容,libfcitxplatforminputcontextplugin.so 需要编译最新的fcitx-qt5。
博主编译的libfcitxplatforminputcontextplugin.so可以下载使用,但是要记得给这个文件可执行权限

参考lieefu的博客:
解决Qt5 Creator无法切换输入法(fcitx),Ubuntu中不能使用搜狗输入法录入汉字问题

二、从源码编译fcitx-qt5

安装cmake和fcitx-libs-dev:

$ sudo apt-get install cmake
$ sudo apt-get install fcitx-libs-dev

根据Qt安装路径设置qmake的环境变量:

$ export PATH="/home/zjl/Qt5.5.1/5.5/gcc_64/bin":$PATH

从github:fcitx/fcitx-qt5下载源码:

$ cd work/
$ git clone https://github.com/fcitx/fcitx-qt5

开始编译:

$ cd work/fcitx-qt5/
$ cmake .
$ make
$ sudo make install
  • 这时候会发现一个关于ECM的报错:
CMake Error at CMakeLists.txt:8 (find_package):
  Could not find a package configuration file provided by "ECM" (requested
  version 1.4.0) with any of the following names:
    ECMConfig.cmake
    ecm-config.cmake
  Add the installation prefix of "ECM" to CMAKE_PREFIX_PATH or set "ECM_DIR"
  to a directory containing one of the above files.  If "ECM" provides a
  separate development package or SDK, be sure it has been installed.
-- Configuring incomplete, errors occurred!

到这个页面 https://launchpad.net/ubuntu/+source/extra-cmake-modules/1.4.0-0ubuntu1 下载 extra-cmake-modules_1.4.0.orig.tar.xz

解压后编译安装:

$ cd work/extra-cmake-modules-1.4.0/
$ cmake .
$ make
$ sudo make install
  • 如果有如下报错
CMake Error at /home/lieefu/Qt5.5.0/5.5/gcc_64/lib/cmake/Qt5Gui/Qt5GuiConfigExtras.cmake:9 (message):
  Failed to find "GL/gl.h" in "/usr/include/libdrm".

安装如下包:

$ sudo apt-get install libgl1-mesa-dev libglu1-mesa-dev

如有如下报错:

CMake Warning at tests/CMakeLists.txt:28 (find_package):
  Could not find a package configuration file provided by "Qt5LinguistTools"
  with any of the following names:

    Qt5LinguistToolsConfig.cmake
    qt5linguisttools-config.cmake

  Add the installation prefix of "Qt5LinguistTools" to CMAKE_PREFIX_PATH or
  set "Qt5LinguistTools_DIR" to a directory containing one of the above
  files.  If "Qt5LinguistTools" provides a separate development package or
  SDK, be sure it has been installed.


-- Looking for Sphinx Documentation Builder...
-- Sphinx Documentation Builder not found - documentation will not be built (see http://sphinx-doc.org/)

按照报错提示将Qt5LinguistTools所在路径添加到环境变量CMAKE_PREFIX_PATH:

$ export CMAKE_PREFIX_PATH="/home/siat/Qt5.5.1/5.5/gcc_64/lib/cmake"
  • 关于XKBCommon的报错
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.26") 
-- Could NOT find XKBCommon_XKBCommon (missing:  XKBCommon_XKBCommon_LIBRARY XKBCommon_XKBCommon_INCLUDE_DIR) 
CMake Error at /usr/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:108 (message):
  Could NOT find XKBCommon (missing: XKBCommon_LIBRARIES XKBCommon) (Required
  is at least version "0.5.0")

解决办法是安装如下软件包:

$ sudo apt-get install bison
$ cd work/
$ wget http://xkbcommon.org/download/libxkbcommon-0.5.0.tar.xz
$ tar xf libxkbcommon-0.5.0.tar.xz
$ cd libxkbcommon-0.5.0/
$ ./configure --prefix=/usr --libdir=/usr/lib/x86_64-linux-gnu --disable-x11
$ make
$ sudo make install

这里需要注意的是lieefu的博客./configure 后面带参数的时候格式写错了,参数前缀是-- 而不是,另外运行./configure 需要cd libxkbcommon-0.5.0/ 文件夹。

装完上面这些之后再重新回到fcitx-qt5进行编译

$ cd work/fcitx-qt5/
$ cmake .
$ make
$ sudo make install

最终我们需要的libfcitxplatforminputcontextplugin.so 文件在/work/fcitx-qt5/platforminputcontext

三、替换libfcitxplatforminputcontextplugin.so 文件

接下来我们只需要将libfcitxplatforminputcontextplugin.so 复制到Qt5.5.1/Tools/QtCreator/bin/plugins/platforminputcontexts/Qt5.5.1/5.5/gcc_64/plugins/platforminputcontexts/ 即可。

为了方便操作,我们将libfcitxplatforminputcontextplugin.so 文件复制到主文件夹~/下,然后运行如下命令

$ cd Qt5.5.1/Tools/QtCreator/bin/plugins/platforminputcontexts/
$ sudo rm libfcitxplatforminputcontextplugin.so 
$ sudo cp ~/libfcitxplatforminputcontextplugin.so .
$ sudo chmod +x libfcitxplatforminputcontextplugin.so 


$ cd Qt5.5.1/5.5/gcc_64/plugins/platforminputcontexts/
$ sudo rm libfcitxplatforminputcontextplugin.so 
$ cp ~/libfcitxplatforminputcontextplugin.so .
$ sudo chmod +x libfcitxplatforminputcontextplugin.so 

注意不要漏掉了cp命令末尾的. 号,rm 命令是为了删除旧的libfcitxplatforminputcontextplugin.so ,另外还需要注意根据自己的Qt安装路径和版本确定platforminputcontexts 文件夹的路径

替换文件之后重新打开Qt就可以在里面Ctrl+空格切换搜狗中文输入法了

如果切换搜狗输入法之后还是无法输入中文就重启一下电脑(不知道为啥,博主的笔记本就是这类干啥都要重启或重开的,另一台台式机不过改啥改完之后不需要重启立马就能用,真是忧伤)

你可能感兴趣的:(Qt,存档,Ubuntu)