Daibingh
Daibingh

Reputation: 41

how to make the applications developed by pyqt5 support Chinese input on ubuntu

On ubuntu, I developed an application using pyqt5, but it does not support Chinese input(My sogouPinyin input method cannot show Chinese context menu when I want to type text on my application). My OS supports Chinese input. How to fix it?

Upvotes: 2

Views: 780

Answers (2)

Daibingh
Daibingh

Reputation: 41

  1. install some packages.
    sudo apt install fcitx-frontend-qt5 fcitx-libs-qt fcitx-libs-qt5
  1. copy lib to pyqt folder.
    sudo cp /usr/lib/x86_64-linux-gnu/qt5/plugins/platforminputcontexts/libfcitxplatforminputcontextplugin.so ${PYTHON_HOME}/lib/site-packages/PyQt5/Qt/plugins/platforminputcontexts/
  1. add os.environ['QT_IM_MODULE'] = 'fcitx' in main file. (maybe not necessary.)

reference

Upvotes: 2

eyllanesc
eyllanesc

Reputation: 243907

sogouPinyin is based on Fcitx, so for the case of Qt applications the docs indicates that you have to use the flag QT_IM_MODULE:

from PyQt5 import QtWidgets

if __name__ == '__main__':
    import sys
    import os
    os.environ['QT_IM_MODULE'] = 'fcitx'
    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QWidget()
    lay = QtWidgets.QVBoxLayout(w)
    lay.addWidget(QtWidgets.QLineEdit())
    lay.addWidget(QtWidgets.QTextEdit())
    w.show()
    sys.exit(app.exec_())

After the application is open you must place the focus in an editing widget such as QLineEdit, QTextEdit and press Ctrl + Space to enable it.

But you have to have installed fcitx-qt5 package:

On Ubuntu:

sudo apt-get install fcitx-qt5 fcitx fcitx-frontend-qt5

Upvotes: 1

Related Questions