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

Detailed explanation of the use of grid properties in CSS

Grid layout Attributes added to the parent elemen...

Docker data management and network communication usage

You can install Docker and perform simple operati...

How to Install Xrdp Server (Remote Desktop) on Ubuntu 20.04

Xrdp is an open source implementation of Microsof...

Solve the problem of docker log mounting

The key is that the local server does not have wr...

How to redirect to https through nginx load balancing

Copy the certificate and key on the web scp -rp -...

How to add, delete and modify columns in MySQL database

This article uses an example to describe how to a...

Vue integrates Tencent TIM instant messaging

This article mainly introduces how to integrate T...

Detailed process of installing and configuring MySQL and Navicat prenium

Prerequisite: Mac, zsh installed, mysql downloade...

Disabled values ​​that cannot be entered cannot be passed to the action layer

If I want to make the form non-input-capable, I se...

How to find websites with SQL injection (must read)

Method 1: Use Google advanced search, for example...