我正在努力获得GMenu
并composite templates
一起工作。
gtk-mumble.vala
using GLib;
using Gtk;
namespace GtkMumble {
[GtkTemplate (ui = "/net/antiochus/gtk-mumble/gtk_mumble.ui")]
public class MainWindow : Gtk.ApplicationWindow {
public MainWindow (Gtk.Application app) {
Object (application: app, title: "gtk-mumble");
var about_action = new SimpleAction ("about", null);
about_action.activate.connect (this.about_cb);
this.add_action (about_action);
this.show ();
}
void about_cb (SimpleAction simple, Variant? parameter) {
print ("This does nothing. It is only a demonstration.\n");
}
[GtkCallback]
public void on_destroy ()
{
application.quit ();
}
}
public class Application : Gtk.Application {
public Application () {
Object (application_id: "net.antiochus.gtk-mumble");
}
protected override void activate () {
assert(this is Gtk.Application);
new MainWindow (this);
}
protected override void startup () {
base.startup ();
var menu = new GLib.Menu ();
menu.append ("About", "win.about");
menu.append ("Quit", "app.quit");
this.app_menu = menu;
var quit_action = new SimpleAction ("quit", null);
quit_action.activate.connect (this.quit);
this.add_action (quit_action);
}
}
int main (string[] args) {
return new Application ().run (args);
}
}
gtk_mumble.ui
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.0 -->
<interface>
<requires lib="gtk+" version="3.0"/>
<template class="GtkMumbleMainWindow" parent="GtkApplicationWindow">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="title" translatable="yes">gtk-mumble</property>
<property name="default_width">499</property>
<property name="default_height">399</property>
<signal name="destroy" handler="on_destroy" swapped="no"/>
<child>
<object class="GtkButton" id="connect_button">
<property name="label" translatable="yes">Connect</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_clicked" swapped="no"/>
</object>
</child>
</template>
</interface>
资源.xml
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/net/antiochus/gtk-mumble">
<file compressed="true" preprocess="xml-stripblanks">gtk_mumble.ui</file>
</gresource>
</gresources>
构建命令:
glib-compile-resources resources.xml --target=resources.c --sourcedir=. --c-name gtk_mumble --generate-source
valac -o gtk-mumble gtk-mumble.vala resources.c --target-glib=2.40 --pkg gtk+-3.0 --pkg gee-0.8 --gresources resources.xml
该代码在运行时导致断言失败:
(gtk_mumble:19708): Gtk-CRITICAL **: gtk_application_get_menubar: assertion 'GTK_IS_APPLICATION (application)' failed
GMenu
不起作用,但 UI 的其余部分起作用(例如,加载了 ui 模板,按钮在那里并且信号起作用)。
当我删除所有[Gtk..]
属性时,GMenu
工作和断言不会失败,但当然 UI 的其余部分不再工作了。
编辑:作为一种解决方法,我正在Gtk.Box
使用模板创建一个并将此框添加到主窗口。那行得通,但我仍然想知道为什么直接方法不起作用。
我使用的版本是:
- Vala 0.22.1(我也试过 0.23.3)
- GLib 2.40.0
- gtk+ 3.12.0