无法使 iconify() 和 deiconify() 正常工作

无法使 iconify() 和 deiconify() 正常工作

我正在使用 C++ 和 Gtkmm 为 Ubuntu 编写应用程序。在我的应用程序中,我使用 libappindicator 在通知区域中显示带有简单菜单的图标。图标和菜单工作正常,我可以通过用户在弹出菜单上执行操作将窗口最小化到面板(使用 iconify() 方法),但 deiconify() 方法不起作用,窗口在面板上保持最小化状态。恢复窗口的唯一方法是单击面板图标手动激活它。

以下是一些代码:

class CMainWindow: public Gtk::Window {
...
    Glib::RefPtr<Gtk::StyleContext>     m_rStyle;
    Glib::RefPtr<Gtk::UIManager>        m_rUIManager;
    Glib::RefPtr<Gtk::ActionGroup>      m_rActionGroup;
...
public:
...
    void                TrayInit();
    void                TrayWinShow();
    void                TrayWinExit();
... 
};

// this method is called from CMainWindow constructor
void CMainWindow::TrayInit() {
    m_rActionGroup = Gtk::ActionGroup::create();

    m_rActionGroup->add(Gtk::Action::create("Show", "Show"),
        sigc::mem_fun(*this, &CMainWindow::TrayWinShow));
    m_rActionGroup->add(Gtk::Action::create("Hide", "Hide"),
        sigc::mem_fun(*this, &CMainWindow::TrayWinHide));

    m_rUIManager = Gtk::UIManager::create();
    m_rUIManager->insert_action_group(m_rActionGroup);
    add_accel_group(m_rUIManager->get_accel_group());

    Glib::ustring strUI =
            "<ui>"
            "  <popup name='IndicatorPopup'>"
                "    <menuitem action='Show' />"
            "    <menuitem action='Hide' />"
            "  </popup>"
            "</ui>";

    m_rUIManager->add_ui_from_string(strUI);

    Gtk::Widget *pWidget;
    pWidget = m_rUIManager->get_widget("/ui/IndicatorPopup");

    m_pAppIcon = app_indicator_new("sample", "sample_icon",
        APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
    app_indicator_set_status(m_pAppIcon, APP_INDICATOR_STATUS_ACTIVE);
    app_indicator_set_menu(m_pAppIcon, GTK_MENU(pWidget->gobj()));
}

void CMainWindow::TrayWinShow() {
    deiconify();
}

void CMainWindow::TrayWinHide() {
    iconify();
}

我还尝试使用 hide()/show() 方法:

void CMainWindow::TrayWinShow() {
    show();
    get_application()->release();
}

void CMainWindow::TrayWinHide() {
    get_application()->hold();
    hide();
}

此变体正确地隐藏了窗口,但是当它调用 show() 时我得到了段错误。

有人能指出我做错什么吗?

答案1

我试过你的iconify()/deiconify()代码,它有效。我添加了最少的代码来让它运行。你能确认这个版本有效吗?

#include <gtkmm.h>
#include <libappindicator/app-indicator.h>
#include <gtkmm/stylecontext.h>

class CMainWindow: public Gtk::Window {
  Glib::RefPtr<Gtk::StyleContext> m_rStyle;
  Glib::RefPtr<Gtk::UIManager>    m_rUIManager;
  Glib::RefPtr<Gtk::ActionGroup>  m_rActionGroup;
  AppIndicator*                   m_pAppIcon;
public:
  void TrayInit();
  void TrayWinShow();
  void TrayWinHide();
  void TrayWinExit();
};

void CMainWindow::TrayInit() {
  m_rActionGroup = Gtk::ActionGroup::create();
  m_rActionGroup->add(Gtk::Action::create("Show", "Show"),
    sigc::mem_fun(*this, &CMainWindow::TrayWinShow));
  m_rActionGroup->add(Gtk::Action::create("Hide", "Hide"),
    sigc::mem_fun(*this, &CMainWindow::TrayWinHide));
  m_rUIManager = Gtk::UIManager::create();
  m_rUIManager->insert_action_group(m_rActionGroup);
  add_accel_group(m_rUIManager->get_accel_group());
  Glib::ustring strUI =
    "<ui>"
    "  <popup name='IndicatorPopup'>"
    "    <menuitem action='Show' />"
    "    <menuitem action='Hide' />"
    "  </popup>"
    "</ui>";
  m_rUIManager->add_ui_from_string(strUI);
  Gtk::Widget *pWidget;
  pWidget = m_rUIManager->get_widget("/ui/IndicatorPopup");
  m_pAppIcon = app_indicator_new("sample", "weather-clear-night",
    APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
  app_indicator_set_status(m_pAppIcon, APP_INDICATOR_STATUS_ACTIVE);
  app_indicator_set_menu(m_pAppIcon, GTK_MENU(pWidget->gobj()));
}

void CMainWindow::TrayWinShow() {
  deiconify();
}

void CMainWindow::TrayWinHide() {
  iconify();
}

int main(int argc, char** argv) {
  Glib::RefPtr<Gtk::Application> app = 
    Gtk::Application::create(argc, argv,"org.gtkmm.examples.base");
  CMainWindow c;
  c.set_default_size(200, 200);
  c.TrayInit();
  return app->run(c);
}

我将其保存test.cpp并编译如下:

g++ test.cpp  `pkg-config --cflags --libs gtkmm-3.0 appindicator3-0.1`

相关内容