【问题标题】:Can I add a global shortcut hook to a Qt application in Ubuntu?我可以在 Ubuntu 中向 Qt 应用程序添加全局快捷方式挂钩吗?
【发布时间】:2011-07-04 20:37:23
【问题描述】:

我的应用程序位于托盘中,我希望它显示其对话框以响应全局热键,例如 Google 桌面搜索。它不必是跨平台的。

【问题讨论】:

    标签: linux qt ubuntu hotkeys


    【解决方案1】:

    Ubuntu 使用GConf 来存储设置。它看起来像 Windows 注册表。 Those keys 用于存储全局快捷方式。要设置全局键控,您应该使用 GTK。所以安装这些包:

    并将以下行添加到您的 .pro 文件中:

    INCLUDEPATH += /usr/include/gconfmm-2.6\
                    /usr/include/glibmm-2.4\
                    /usr/lib/glibmm-2.4/include\
                    /usr/include/glib-2.0\
                    /usr/lib/glib-2.0/include\
                    /usr/include/sigc++-2.0\
                    /usr/lib/sigc++-2.0/include\
                    /usr/include/gconf/2
    
    LIBS += -L/usr/lib -lgconfmm-2.6
    

    那么你需要创建新的.cpp 文件或类,因为你不能在同一个文件中包含 Qt 和 GTK 头文件。

    #include <gconfmm.h>
    using namespace Gnome;
    

    和主要部分:

    Conf::init();
        try
        {
            Glib::RefPtr<Conf::Client> pointer(Conf::Client::get_default_client());
            pointer->set("/apps/metacity/keybinding_commands/command_2",Glib::ustring("gedit"));
            pointer->set("/apps/metacity/global_keybindings/run_command_2",Glib::ustring("<Control><Alt>e"));
        }
        catch(Conf::Error error)
        {
            std::cerr << "Error code: " << error.code();
        }
    

    您可以使用所有预定义的密钥来代替command_2(例如command_screenshot),您可以创建自己的密钥,例如

    /apps/metacity/keybinding_commands/command_textedit
    /apps/metacity/global_keybindings/run_command_textedit
    

    但您还需要为自定义键设置模式(我不知道该怎么做,我是 GTK 新手)。执行代码后,快捷方式运行良好。

    【讨论】:

      【解决方案2】:

      在 SO 中搜索 [qt] 全局热键 会得到以下结果:

      Trouble with QxtGlobalShortcut [solved]

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-18
        • 1970-01-01
        • 1970-01-01
        • 2011-11-23
        • 1970-01-01
        相关资源
        最近更新 更多