Input Chinese in Qt5 program

QT

The reason is that under Debian 10 (Buster), I have been unable to input Chinese under QT applications, such as wps/libreoffice/wine. I have search it with google for a long time, but hasn’t found the resolve method. I found the solution by accident, so this article for people who have the same problem.

Backgroud

  • OS: Debian Buster x64
  • Language: en_US.UTF-8
  • Desktop: MATE Desktop 1.22.2
  • Input: Fcitx v4.2.9.7

After switching to Debian Buster, I found that QT applications cannot switch input methods to Chinese, such as programs installed in wine (notepad / office 2007 / libreOffice / wps). But the other programs can switch input methods for Chinese input, such as Firefox / VIM / Leafpad, etc.

Solution

I have search it with Baidu/Google for a long time, basically add variables to specify fcitx or recompile fcitx-qt5.

And I use the third method to solve this problem.

One. Add environment variables

Add the following variables in .bashrc/.xprofile/.xinputrc/.profile or write a script.

1
2
3
4
export GTK_IM_MODULE=fcitx
export QT_IM_MODULE=fcitx
export QT4_IM_MODULE=fcitx
export XMODIFIERS=@im=fcitx

Two. Compile fcitx-qt5 from source

The official repository of fcitx-qt5 in Linux is too old and is not compatible with QT5.6 and above. You need to recompile it in the new version of QT.

Note: Do not omit the following steps, otherwise some errors may occur during compilation.

1. Install dependencies

1
2
:~$ sudo apt-get install cmake fcitx-libs-dev extra-cmake-modules
:~$ wget

2. Install QT5 and fcitx-qt5s’ source

Download and install Web - QT5 locally.
Here we download QT5.14.1 for linux 64-bits

1
2
3
:~$ wget http://download.qt.io/official_releases/qt/5.14/5.14.1/qt-opensource-linux-x64-5.14.1.run
:~$ chmod +x qt-opensource-linux-x64-5.14.1.run
:~$ ./qt-opensource-linux-x64-5.14.1.run

During the installation process, you first need to register on the QT website, and then you will encounter a check box. If you don’t know which one to install, install them all.

1
:~$ git clone https://github.com/fcitx/fcitx-qt5

3. Start compiling

1
2
3
4
5
:~$ export PATH="/home/root/Qt5.14.1/5.14.1/gcc_64/bin":$PATH
:~$ cd fcitx-qt5
:~$ cmake .
:~$ make
:~$ sudo make install

Fortunately, we have compiled successfully, and here we need libfcitxplatforminputcontextplugin.so.
If you get an error, please Google resolve it by yourself.

4. Overwrite the original module

1
2
:~$ mv /usr/lib/x86_64-linux-gnu/qt5/plugins/platforminputcontexts/libfcitxplatforminputcontextplugin.so /usr/lib/x86_64-linux-gnu/qt5/plugins/platforminputcontexts/libfcitxplatforminputcontextplugin.so.old
:~$ cp fcitx-qt5/platforminputcontext/libfcitxplatforminputcontextplugin.so /usr/lib/x86_64-linux-gnu/qt5/plugins/platforminputcontexts/

Three. Switch to Chinese locale

I accidentally changed the language variable in locale, then fcitx can be used normally in the QT program!

Take LibreOffice as an example:

1
2
3
4
5
6
7
8
9
:~$ cat > office <<EOF
!#/sbin/bash

export LANG=zh_CN.UTF-8
killall fcitx
fcitx && libreoffice
EOF
:~$ chmod +x office
:~$ office

I wrote a script, first set the locale environment variable to Chinese, then close the existing fcitx, and then reopen fcitx and libreoffice. At this time, you can enter Chinese in LibreOffice.

In order to practice my English, I don’t want to switch back to the Chinese locale. So I used the above method to write a script for the QT5 program.

Why programs other than QT programs can enter Chinese in an English environment?


Prefences:

  • CSDN
  • fcitx
  • qt web