因此,我想我应该是更通用,更易于使用的类之一,即用于ListBox数据的Gee.ArrayList。事实证明,ListBox将采用一个ListModel,而且我认为,因为我使用的是ArrayList,所以我最好只创建一个既是Gee.ArrayList又是ListModel的类:
public class ObservableArrayList<T> : ListModel, Gee.ArrayList<T>{
//Implement ListModel
public Object? get_item(uint position){
if((int)position > size){
return null;
}
return (Object?) this.get((int)position);
}
public Type get_item_type(){
return element_type;
}
public uint get_n_items(){
return (uint)size;
}
public new Object? get_object(uint position){
if((int)position > size){
return null;
}
return (Object) this.get((int)position);
}
}
但是,这给了我一个奇怪的编译信息:
/home/rasmus/Projects/Vala/Test/ObservableList.vala.c: In function ‘observable_array_list_g_list_model_interface_init’:
/home/rasmus/Projects/Vala/Test/ObservableList.vala.c:189:18: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
iface->get_item = (GObject* (*) (GListModel*, guint)) observable_array_list_real_get_item;
尽管编译成功,但该类非常无法用作ListModel:
using Gtk;
public class TestApp : Gtk.Application{
public TestApp () {
Object (
application_id: "TestApp",
flags: ApplicationFlags.FLAGS_NONE
);
}
protected override void activate(){
var main_window = new Gtk.ApplicationWindow (this);
main_window.default_height = 400;
main_window.default_width = 600;
main_window.title = "test";
ListModel t = new ObservableArrayList<int>();
var list_box = new Gtk.ListBox();
list_box.bind_model(t, null);
main_window.add(list_box);
main_window.show_all ();
}
public static int main (string[] args) {
Gtk.init (ref args);
var app = new TestApp ();
return app.run(args);
}
}
当尝试运行已编译的程序时,输出为:
segmentationfault
是否有解决此问题的好方法,或者我从一开始就尝试过错误的事情?
答案 0 :(得分:2)
要记住的重要一件事是Vala实际上会编译为C,然后将其馈送到GCC中以生成可执行文件,您的编译器警告实际上是从gcc
而不是valac
在我的机器上,消息的格式略有不同
warning: assignment to ‘void * (*)(GListModel *, guint)’ {aka ‘void * (*)(struct _GListModel *, unsigned int)’} from incompatible pointer type ‘GObject * (*)(GListModel *, guint)’ {aka ‘struct _GObject * (*)(struct _GListModel *, unsigned int)’}
可以简化为
assignment to ‘void * (*)(GListModel *, guint)’ from incompatible type ‘GObject * (*)(GListModel *, guint)’
基本上说GLib期望get_item返回void *
而不是GObject,这是绑定中的错误,因此可以忽略
运行附带运行时警告
(list:4511): GLib-GIO-CRITICAL **: 21:44:24.003: g_application_set_application_id: assertion 'application_id == NULL || g_application_id_is_valid (application_id)' failed
(list:4511): Gtk-CRITICAL **: 21:44:24.008: gtk_list_box_bind_model: assertion 'model == NULL || create_widget_func != NULL' failed
所以你有2个问题
com.githost.me.App
但是这些都没有告诉我们为什么要获得SEGV
答案在于,您的GListModel包含类型为int
的元素,而GtkListBox则期望为Object
的集合