Quickly solve the Chinese input method problem under Linux

Quickly solve the Chinese input method problem under Linux

Background: I'm working on asset reporting related functions recently, and I need to support Chinese input. If the program is started with a normal shortcut, there is no problem. However, when upgrading or uninstalling and reinstalling, it is started with su usr -C XX.sh. It is impossible to obtain the user's environment variables when started under root. Let's start our adventure. . .

Exploration: By comparing the environment variables of root and user through export printing, and searching for issues related to input methods under linux in google, I found the variable QT_IM_MODULE. Different systems may have ibus or fctix or other variables, etc.

Solution: Because the program needs to be started manually by the user when it is installed for the first time, we need to add the code to get the value of QT_IM_MODULE under the current user, and then write it to a global file, and then get the value of this global variable in the hook script. In the startup script, just set export QT_IM_MODULE=XX. The code is shown below.

The code has something to say:

1. Code that needs to be added to the program:

//Add global Chinese input method environment variable QString qstrInputMethod = path;
    qstrInputMethod += "/.local/sdforcnos/sdforcnos.inputMethod";
    char *pInputEnv = getenv("QT_IM_MODULE");
    if(pInputEnv != NULL)
    {
      QString qstrCMD = QString("echo %1 > %2").arg(pInputEnv).arg(qstrInputMethod);
      system(qstrCMD.toStdString().c_str());
      printf("---%s-----------QT_IM_MODULE=%s\n", qstrCMD.toStdString().c_str(), pInputEnv);
    }

2. Code required by the hook script:

 if [ -e "/home/${user}/.local/sdforcnos/sdforcnos.inputMethod" ];then
     inputEnv=`cat /home/${user}/.local/sdforcnos/sdforcnos.inputMethod`
     if [ ! -z "$inputEnv" ]; then
       export QT_IM_MODULE="$inputEnv"
     fi
    fi
    su $user -c "/usr/bin/startsd &"

Summarize

This is the end of this article about Chinese input method problems under Linux. For more relevant content about Chinese input method problems under Linux, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Trash-Cli: Command-line Recycle Bin Tool on Linux
  • 101 scripts to create a Linux recycle bin script
  • Linux uses suid vim.basic file to achieve privilege escalation
  • Use MySQL to open/modify port 3306 and open access permissions in Ubuntu/Linux environment
  • Detailed explanation of commands to read and write remote files using Vim in Linux system
  • Solution to Linux server graphics card crash
  • Detailed explanation of the implementation process and usage of the Linux Recycle Bin mechanism

<<:  Vue implements adding watermark to uploaded pictures

>>:  Vue realizes adding watermark to uploaded pictures (upgraded version)

Recommend

How to use ssh tunnel to connect to mysql server

Preface In some cases, we only know the intranet ...

MySQL database optimization: index implementation principle and usage analysis

This article uses examples to illustrate the prin...

How to modify the sources.list of Ubuntu 18.04 to Alibaba or Tsinghua mirror

1. Backup source list The default source of Ubunt...

Creative opening effect achieved by combining CSS 3.0 with video

Let me share with you a creative opening realized...

How to download excel stream files and set download file name in vue

Table of contents Overview 1. Download via URL 2....

How to use docker+devpi to build local pypi source

Some time ago, I needed to use pip downloads freq...

Introduction to installing and configuring JDK under CentOS system

Table of contents Preface Check and uninstall Ope...

Vue implements a simple calculator

This article example shares the specific code of ...

A brief analysis of MySQL explicit type conversion

CAST function In the previous article, we mention...

Summary of the benefits of deploying MySQL delayed slaves

Preface The master-slave replication relationship...

Summary of commonly used CSS encapsulation methods

1. pc-reset PC style initialization /* normalize....

Linux CentOS6.9 installation graphic tutorial under VMware

As a technical novice, I am recording the process...

A brief discussion on the VUE uni-app development environment

Table of contents 1. Through HBuilderX visual int...