当我尝试使用Glade在Vala中将GTK IconView子类化时,出现分段错误。这是Vala中的错误,还是我做错了什么?这使用的是vala 0.42.3。也许这与IconView如何没有base()
构造函数有关? (请参阅:Chain up to 'Gtk.Box.new' not supported)
test.vala:
using Gtk;
public class IconViewSubclass : Gtk.IconView {
public IconViewSubclass() {
}
}
public static int main(string[] args) {
Gtk.init(ref args);
var builder = new Builder.from_file("test.glade");
var window = builder.get_object("window") as Window;
var iconViewSubclass = builder.get_object("iconViewSubclass") as IconViewSubclass;
iconViewSubclass.set_pixbuf_column(0);
iconViewSubclass.set_text_column(1);
window.show_all();
Gtk.main();
return 0;
}
test.glade:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkListStore" id="store">
<columns>
<!-- column-name pixbuf -->
<column type="GdkPixbuf"/>
<!-- column-name text -->
<column type="gchararray"/>
</columns>
</object>
<object class="GtkWindow" id="window">
<property name="can_focus">False</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkIconView" id="iconViewSubclass">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin">6</property>
<property name="model">store</property>
</object>
</child>
</object>
</interface>
$ valac --pkg gtk+-3.0 test.vala && ./test
Segmentation fault
答案 0 :(得分:1)
您似乎需要使用IconViewSubclass
让Gtk.Builder知道expose_object()
存在。这允许在Builder UI定义文件中使用子类型。这是一个编译但不存在段错误的示例:
test.ui
:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.18"/>
<object class="GtkWindow" id="window">
<property name="window-position">GTK_WIN_POS_CENTER</property>
<property name="default-height">250</property>
<property name="default-width">250</property>
<child>
<placeholder/>
</child>
<child>
<object class="IconViewSubclass" id="iconViewSubclass">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="margin">6</property>
</object>
</child>
</object>
</interface>
和test.vala
:
using Gtk;
public class IconViewSubclass : Gtk.IconView {}
public static int main(string[] args) {
Gtk.init(ref args);
var builder = new Builder ();
builder.expose_object ("IconViewSubclass", new IconViewSubclass ());
try {
builder.add_from_file ("test.ui");
} catch (Error error) {
print (@"$(error.message)");
}
var window = builder.get_object ("window") as Window;
var iconViewSubclass = (IconViewSubclass)builder.get_object ("iconViewSubclass");
iconViewSubclass.set_pixbuf_column (0);
iconViewSubclass.set_text_column (1);
window.show_all();
Gtk.main();
return 0;
}
您可能想研究使用具有Vala [GtkTemplate]
,[GtkChild]
和[GtkCallback]
属性的模板。这些属性将告诉Vala为您生成样板代码。
答案 1 :(得分:0)
AIThomas的代码效果很好,但是,为了继续在Glade中编辑UI文件,我必须添加一个目录文件,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<glade-catalog name="test" depends="gtk+">
<glade-widget-classes>
<glade-widget-class name="IconViewSubclass" generic-name="iconviewsubclass" title="IconViewSubclass" parent="GtkIconView"/>
</glade-widget-classes>
<glade-widget-group name="test" title="test">
<glade-widget-class-ref name="IconViewSubclass"/>
</glade-widget-group>
</glade-catalog>
我还发现, Vala中的子类名称必须与UI XML中的小部件名称完全对应,即将IconViewSubclass
中的IconViewSubclass2
更改为test.vala
将给您:
$ valac --pkg gtk+-3.0 test.vala && ./test
test.ui:14:1 Invalid object type 'IconViewSubclass'Segmentation fault
您还可以将名称空间用作类名的一部分,即namespace Foo { public class Bar : Baz }
应该适用于UI XML文件中类型为FooBar
的小部件。