当我尝试使用 Glade 在 Vala 中子类化 GTK IconView 时,我遇到了段错误。这是 Vala 中的错误,还是我只是做错了什么?这是使用 vala 0.42.3。也许这与 IconView 没有 base()
构造函数有关? (参见:Chain up to 'Gtk.Box.new' not supported)
测试.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;
}
测试.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
最佳答案
看起来您需要使用 expose_object()
让 Gtk.Builder 知道 IconViewSubclass
存在。这允许在 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 为您生成样板代码。
关于oop - 如何使用 Glade 在 Vala 中子类化 GTK IconView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54278014/