When I try to subclass a GTK IconView in Vala using Glade, I get a segmentation fault. Is this a bug in Vala, or am I just doing something wrong? This is using vala 0.42.3. Maybe this is related to how IconView doesn't have a base()
constructor? (see: 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
It looks like you need to let the Gtk.Builder know IconViewSubclass
exists using expose_object()
. This allows the sub-type to be used in the Builder UI definition file. Here's an example that compiles and does not segfault:
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>
and 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;
}
You may want to look into using templates with Vala [GtkTemplate]
, [GtkChild]
and [GtkCallback]
attributes. The attributes will tell Vala to generate the boiler plate code for you.