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

Solution to occasional crash of positioning background service on Linux

Problem Description In the recent background serv...

Comparing Node.js and Deno

Table of contents Preface What is Deno? Compariso...

Steps to configure IIS10 under Win10 and support debugging ASP programs

Microsoft IIS IIS (Internet Information Server) i...

WeChat applet implements countdown for sending SMS verification code

This article shares the specific code for the WeC...

In-depth analysis of nginx+php-fpm service HTTP status code 502

One of our web projects has seen an increase in t...

How to choose transaction isolation level in MySQL project

introduction Let's start with our content. I ...

MySql import CSV file or tab-delimited file

Sometimes we need to import some data from anothe...

Complete steps to build NFS file sharing storage service in CentOS 7

Preface NFS (Network File System) means network f...

Teach you MySQL query optimization analysis tutorial step by step

Preface MySQL is a relational database with stron...

DHTML objects (common properties of various HTML objects)

!DOCTYPE Specifies the Document Type Definition (...

Various types of MySQL indexes

What is an index? An index is a data structure th...

Vue parent component calls child component function implementation

Vue parent component calls the function of the ch...

javascript:void(0) meaning and usage examples

Introduction to void keyword First of all, the vo...

React antd tabs switching causes repeated refresh of subcomponents

describe: When the Tabs component switches back a...