big_worm 2024-10-07 01:49 采纳率: 0%
浏览 29
已结题

基于信创PC发布的QT应用如何跨用户启动后输入中文

我正在使用QT在信创PC机上开发应用程序,因为PC机的特殊使用需要,我作为运维人员需要通过开机设置启动专用应用程序,即使用root用户启动界面的应用程序,实际的使用者将在thtf用户下使用PC机上的应用。目前功能已经实现了,但是却无法输入中文,我采用的解决方式是:
1、在执行程序的目录中复制了开发所用QT版本的plugins目录;
2、在启动脚本中设置了lib目录和plugins目录位置;
3、在启动脚本中设置了输入法类型:fctix;

具体的启动脚本如下:


```bash
#!/bin/bash

# Allow reader to access the X server
xhost +SI:localuser:reader

# Switch to reader user and run the command as reader
sudo -u reader bash << 'EOF'

# Set DISPLAY and XAUTHORITY for reader user
export DISPLAY=:0
export XAUTHORITY=/home/thtf/.Xauthority

# Set locale for Chinese input
export LANG=zh_CN.UTF-8
export LC_ALL=zh_CN.UTF-8

# Set XDG_RUNTIME_DIR for reader user (UID 1001)
export XDG_RUNTIME_DIR=/run/user/$(id -u)

# Ensure necessary runtime directory exists and has correct permissions (this part runs as reader)
mkdir -p $XDG_RUNTIME_DIR
chmod 0700 $XDG_RUNTIME_DIR

# Ensure necessary libraries and plugins are loaded
export LD_LIBRARY_PATH=/home/reader/RM:$LD_LIBRARY_PATH
export QT_QPA_PLATFORM_PLUGIN_PATH=/home/reader/RM/plugins
export QT_QPA_PLATFORM=xcb

# Set input method environment variables for fcitx
export XMODIFIERS=@im=fcitx
export QT_IM_MODULE=fcitx
export GTK_IM_MODULE=fcitx

# Ensure fcitx is running (uncomment if necessary)
# fcitx &

# Start D-Bus if not running
eval $(dbus-launch)
export DBUS_SESSION_BUS_ADDRESS

# Launch the program with debugging enabled (uncomment if debugging is needed)
# QT_DEBUG_PLUGINS=1 /home/reader/ReadingRoom/MouseKeyEvent > /home/reader/debug_log.txt 2>&1

# Launch the MouseKeyEvent program
/home/reader/RM/MouseKeyEvent

EOF


  • 写回答

28条回答 默认 最新

  • 阿里嘎多学长 2024-10-07 01:49
    关注
    获得0.30元问题酬金

    AIGC生成:


    基于信创PC发布的QT应用如何跨用户启动后输入中文

    你好!我是阿里嘎多学长。

    你的问题是:如何在信创PC机上使用QT应用程序,跨用户启动后输入中文?

    解决方案:

    1. 在QT应用程序中使用 QApplication::setDesktopFileName 函数设置应用程序的桌面文件名,以便在不同的用户下启动应用程序。
    2. /etc/sudoers 文件中添加以下配置,允许 thtf 用户使用 sudo 命令:
    thtf ALL=(root) NOPASSWD: /path/to/your/app
    
    1. 在应用程序的启动脚本中使用 sudo 命令来启动应用程序:
    sudo -u root /path/to/your/app
    

    这样,应用程序将以 root 用户身份启动,并且可以输入中文。

    代码示例:

    #include <QApplication>
    
    int main(int argc, char *argv[]) {
        QApplication app(argc, argv);
        app.setDesktopFileName("your_app_name");
        // ...
        return app.exec();
    }
    
    #!/bin/bash
    
    sudo -u root /path/to/your/app
    

    注意:在使用 sudo 命令时,需要确保 thtf 用户的密码已经设置,并且 /etc/sudoers 文件已经更新。


    评论

报告相同问题?

问题事件

  • 系统已结题 10月15日
  • 创建了问题 10月7日