我一直在使用gtkmm编写Gtk +应用程序,我正在尝试添加一个调用回调的全局键盘快捷键。不幸的是,Gtk :: AccelGroup的connect()方法在gtkmm中不可用,显然是故意的,因为你可以使用ActionGroups建立连接......
无论如何,我有以下代码:
actions_= Gtk::ActionGroup::create();
actions_->set_accel_group(Gtk::AccelGroup::create());
actions_->add(
Gtk::Action::create("new"), Gtk::AccelKey("<control>n"),
sigc::mem_fun(this, &Window::new_buffer_thing)
);
_gtk_window().add_accel_group(actions_->get_accel_group());
在没有警告的情况下编译并运行,但键盘快捷方式不执行任何操作。我已经花了好几个小时摆弄这个,所以任何帮助或方向都会受到赞赏!
我做错了什么吗?为什么加速器不起作用?
答案 0 :(得分:0)
回答这个问题有点晚了,但我今天也在解决同样的问题,即使在不同的环境中:python,gtk2。
根据我对this tutorial的一些小实验的理解,除非与工具箱或菜单栏相关联,否则操作不会处于活动状态。太糟糕了,就这样做,将工具栏打包成一个VBox,让它看不见,就像这样:
resources :votes
知道相同的方法是否有助于OP会很有趣。
答案 1 :(得分:0)
阅读mariotomo的答案后,这是我对gtkmm3的隐藏菜单栏的实现。
快捷键加速器可用于隐藏的菜单栏:)
#include <iostream>
#include <gtkmm.h>
using Gtk::Application;
using Gtk::Box;
using Gtk::Builder;
using Gio::SimpleActionGroup;
using std::cout;
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow(const Glib::RefPtr<Application>& app);
private:
void on_action_file_new();
// for hidden menubar
void on_action_secret();
Box box1;
Box box2;
Glib::RefPtr<Builder> builder;
Glib::RefPtr<SimpleActionGroup> actiongroup;
};
ExampleWindow::ExampleWindow(const Glib::RefPtr<Application>& app)
: box1(Gtk::ORIENTATION_VERTICAL)
, box2(Gtk::ORIENTATION_VERTICAL)
{
set_title("Menubar test");
set_default_size(400, 200);
add(box1);
box1.add(box2);
//Define the actions:
actiongroup = Gio::SimpleActionGroup::create();
actiongroup->add_action("new" , sigc::mem_fun(*this, &ExampleWindow::on_action_file_new));
actiongroup->add_action("secret", sigc::mem_fun(*this, &ExampleWindow::on_action_secret));
insert_action_group("example" , actiongroup);
insert_action_group("example-hidden", actiongroup);
builder = Gtk::Builder::create();
app->set_accel_for_action("example.new" , "<Primary>n");
app->set_accel_for_action("example-hidden.secret", "<Primary>m");
const char *visible_ui_info =
"<interface>"
" <menu id='visible-menubar'>"
" <submenu>"
" <attribute name='label' translatable='yes'>_File</attribute>"
" <section>"
" <item>"
" <attribute name='label' translatable='yes'>_New</attribute>"
" <attribute name='action'>example.new</attribute>"
" <attribute name='accel'><Primary>n</attribute>"
" </item>"
" </section>"
" </submenu>"
" </menu>"
"</interface>";
const char *hidden_ui_info =
"<interface>"
" <menu id='hidden-menubar'>"
" <submenu>"
" <attribute name='label' translatable='yes'>_Secret</attribute>"
" <section>"
" <item>"
" <attribute name='label' translatable='yes'>_Key</attribute>"
" <attribute name='action'>example-hidden.secret</attribute>"
" <attribute name='accel'><Primary>m</attribute>"
" </item>"
" </section>"
" </submenu>"
" </menu>"
"</interface>";
builder->add_from_string(visible_ui_info);
builder->add_from_string(hidden_ui_info);
// Get the visible menubar:
auto object1 = builder->get_object("visible-menubar");
auto gmenu1 = Glib::RefPtr<Gio::Menu>::cast_dynamic(object1);
auto menubar1 = Gtk::make_managed<Gtk::MenuBar>(gmenu1);
box1.pack_start(*menubar1, Gtk::PACK_SHRINK);
// Get the hidden menubar
auto object2 = builder->get_object("hidden-menubar");
auto gmenu2 = Glib::RefPtr<Gio::Menu>::cast_dynamic(object2);
auto menubar2 = Gtk::make_managed<Gtk::MenuBar>(gmenu2);
box2.pack_start(*menubar2, Gtk::PACK_SHRINK);
show_all_children();
box2.hide();
}
void ExampleWindow::on_action_file_new()
{
cout << "New menu item was selected.\n";
}
void ExampleWindow::on_action_secret()
{
cout << "Secret menu item was selected.\n";
}
int main()
{
auto app = Application::create("org.gtkmm.example");
ExampleWindow window(app);
return app->run(window);
}