Quickly solve the problem of Chinese input method under linux

  • 2021-08-21 21:51:42
  • OfStack

Story background: Recently, I am doing asset reporting related functions, and I want to support Chinese input. If the normal shortcut startup program is fine, but upgrade or uninstall and reinstall, I will use su usr-C XX. sh to start when I start, and I can't get the user's environment variables when I start under root. Let's start our exploration. . .

The way to explore: By comparing the environment variables printed by export with root under users, and the input method related to google under linux, the variable QT_IM_MODULE is found. Different systems will be ibus or fctix or others,

Solution: Because the first installation of the program requires the user to start manually, we need to add the value of QT_IM_MODULE under the current user in the code, 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, it is OK to set export QT_IM_MODULE=XX, and the following code is displayed

The code has something to say:

1. The code that needs to be added in the program:


// Add Global Chinese Input Method Environment Variables 
    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 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


Related articles: