250612-supportInputChineseInQtApp

250612-supportInputChineseInQtApp

eton@250612-supportInputChineseInQtApp first editon for support input chinese in a Qt6 application;

Background:

在支持了ibus输入法在Debian12安装以后,需要确认开发的程序是否支持中文输入, 然后要支持输入中文。
有几个前提:

  1. Debian12 locale is "LANG=en_HK.UTF-8, LANGUAGE=en_HK:en"; 'XDG_SESSION_TYPE=wayland, XDG_SESSION_DESKTOP=gnome';
    1. input method framework is fcitx5;
    1. qtcreator install from qt.io cannot input chinese;
    1. new created demo app with a TextEdit cannot input chinese also;

Contents:

最后的结果是依据Using_Fcitx_5_on_Wayland的方法QT_QPA_PLATFORM=xcb QT_IM_MODULE=ibus # Enforce it to run on X11/XWayland and use ibus im module成功解决了不能激活输入法的问题。

下面记录整个过程.

1. 确认互联网上面内容的正确性

csdn等中文blog中的信息主要包含如下过程:

  1. export _IM_MODULE一堆可能相关的环境变量;
  2. 手动编译fcitx-qt源代码然后本地安装;

cp ~/fcitx-qt5-master/build/qt5/platforminputcontext/libfcitxplatforminputcontextplugin-qt5.so ~/Qt5.14.2/5.14.2/gcc_64/plugins/platforminputcontexts/
~/Qt5.14.2/Tools/QtCreator/lib/Qt/plugins/platforminputcontexts/

整体的内容不能说错误,但是操作内容大部分属于别处的简单重复和抄录,存在下面主要的问题:

  1. 没有分析实际问题根源;
  2. 无用操作太多,副作用太大,容易导致本地环境被严重破坏;
  3. 成本抬高: 1. 需要自己下载-编译代码; 2. 需要修改OS层配置文件/etc/profile; 3. 需要添加太多不必要的环境变量而且还是export;

2. 解决qtcreator中文输入问题

  1. 1st try:
#!/bin/bash
export QT_IM_MODULE=fcitx XMODIFIERS=@im=fcitx GTK_IM_MODULE=fcitx
QT_IM_MODULE=wayland LANG=zh_CN.UTF-8 LANGUAGE=zh_CN:zh \
LC_ALL=zh_CN.UTF-8 LANG=zh_CN.UTF-8 QT_QPA_PLATFORM=wayland /home/eton/Qt/Tools/QtCreator/bin/qtcreator

这样的结果是1. qtcreator默认显示中文; 2. 很多内容排版出现错误,主要是内置terminal因为全宽字符导致空白特别大; 3. 依然不能输入中文, 主要是无法激活输入法;

  1. 2cd try:
QT_IM_MODULE=wayland QT_QPA_PLATFORM=wayland \
/home/eton/Qt/Tools/QtCreator/bin/qtcreator

it work! 因为当前自己的环境就是这样的;

3. qt app demo

创建一个新的app以排除qtcreator可能有其他内部配置导致的问题.

  1. 先直接利用前面qtcreator的方法到app
  • QT_IM_MODULE=wayland ./appqt6-input-chinese failed;
  • QT_IM_MODULE=wayland QT_QPA_PLATFORM=wayland ./appqt6-input-chinese failed;

查看本地qt环境中默认包含的对输入法支持插件:

$ ls ~/Qt/6.5.3/gcc_64/plugins/platforminputcontexts/ |grep -v debug
libcomposeplatforminputcontextplugin.so
libibusplatforminputcontextplugin.so
libqtvirtualkeyboardplugin.so
  1. read the fcitx5 wiki:Using Fcitx 5 on Wayland--QT_IM_MODULE

QT_IM_MODULE
For QT_IM_MODULE, as for now QT can only use its own text-input-v{2,4}, which is only supported by KWin. Which means, under KDE you should unset it, but under other desktop, you will need to set it to QT_IM_MODULE=fcitx.
Also the proprietary Qt applications in the wild complicates the situation. Some do not work on Wayland, some do not bundle/provide Qt wayland. Most of them do not bundle fcitx im module because fcitx is only a third party application to Qt, but I also noticed some do bundle but not bundle all the required libraries.
While fcitx 5 support ibus protocol, some of those applications do not even bundle ibus im module.
There are a few different environment variables you can try for those proprietary Qt applications (WPS, Anki, DaVinci Resolve, Mathematica, etc...).

QT_IM_MODULE=fcitx # For those who bundle qt im module, e.g. WPS, Anki, you should find a .so file with fcitx in the file name
QT_IM_MODULE=ibus # For those who bundle ibus im module shipped with Qt, you should find libibusplatforminputcontextplugin.so in the package.
QT_QPA_PLATFORM=xcb QT_IM_MODULE=ibus # Enforce it to run on X11/XWayland and use ibus im module

In Qt 6.7, there is a new environment variable called "QT_IM_MODULES", which allows you so specify a fallback order on im modules. You can set it to QT_IM_MODULES="wayland;fcitx;ibus"
So it will be able pick up the most usable one even for application that bundles no fcitx/wayland. Remember, you may still need to set( or unset) QT_IM_MODULE (not "QT_IM_MODULES") in order to handle Qt 4/5 applications.

  1. try again:
QT_IM_MODULE=ibus ./appqt6-input-chinese

it work; 根本原因就是~/Qt/6.5.3/gcc_64/plugins/platforminputcontexts/中只有ibus的plugin, 而且fcitx5支持ibus协议;

  • attention:'QT_IM_MODULE=fcitx' 'QT_IM_MODULE=""' 都不能工作

4. screedDisplay

$ QT_IM_MODULE=ibus ./bin/screenDisplay

work. 根本原因就是~/Qt/6.5.3/gcc_64/plugins/platforminputcontexts/中只有ibus的plugin, 而且fcitx5支持ibus协议;

  • attention:因为打包环境中只有xcb作为QT_QPA_PLATFORM所以QT_QPA_PLATFORM=xcb或者不指定都可以,但是如果设置QT_QPA_PLATFORM=wayland会出错; //但是自己本地编译环境不会,因为jenkins是ubuntu + X11环境, deploy时候没有包含wayland;

下面是qt环境支持的内容,包含xcb+wayland:

$ ls ~/Qt/6.5.3/gcc_64/plugins/platforms/ |grep -v debug
libqeglfs.so
libqlinuxfb.so
libqminimalegl.so
libqminimal.so
libqoffscreen.so
libqvkkhrdisplay.so
libqvnc.so
libqwayland-egl.so
libqwayland-generic.so
libqxcb.so

5. screendisplay in siho

所有主要做的就是在deploy make package的时候将插件引入,然后什么也不配置就可以输入中文,因为测试设备中原本就安装了ibus;

plugins/platforminputcontexts/
└── libibusplatforminputcontextplugin.so

1 directory, 1 file

Appendix:

Ref:

ls /opt/calibre/lib/qt_plugins/platforminputcontexts
libcomposeplatforminputcontextplugin.so libibusplatforminputcontextplugin.so
可见,Calibre自带了IBus插件,能使用IBus框架下的输入法,却没有Fcitx对应的插件。我们要做的就是把fcitx-libs-qt5中的动态库文件拷贝到/opt/calibre/lib/目录下,将fcitx-frontend-qt5中的动态库文件拷贝到/opt/calibre/lib/qt_plugins/platforminputcontexts/目录下。

cp ~/fcitx-qt5-master/build/qt5/platforminputcontext/libfcitxplatforminputcontextplugin-qt5.so ~/Qt5.14.2/5.14.2/gcc_64/plugins/platforminputcontexts/
~/Qt5.14.2/Tools/QtCreator/lib/Qt/plugins/platforminputcontexts/

250612-supportInputChineseInQtApp.end

posted on 2025-06-12 10:56  Eton..Bee  阅读(45)  评论(1)    收藏  举报