所以我想我会是聪明的更通用和易于使用的类,即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
有没有办法解决这个好办法,还是我一直在努力的东西,从一开始就错了吗?
需要记住的重要一点是,瓦拉实际上编译到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,而不是返回的GObject的void *
,这是在映射,所以可以忽略的错误
运行自带的运行时警告
(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包含而GtkListBox期待int
的集合型Object
的元素