私はvalaクラスでこれの振る舞いを扱うのにほとんど問題がありません。
これが私のコードです:(build_and_send1とbuild_and_send2はボタンを押すことによって発生するシグナルです)
using GLib;
using Gtk;
public class Main : Object
{
/*
* Uncomment this line when you are done testing and building a tarball
* or installing
*/
//const string UI_FILE = Config.PACKAGE_DATA_DIR + "/" + "gtk_httpclient.ui";
const string UI_FILE = "src/gtk_httpclient.ui";
/* ANJUTA: Widgets declaration for gtk_httpclient.ui - DO NOT REMOVE */
private Builder builder;
public Main ()
{
try
{
this.builder = new Builder ();
this.builder.add_from_file (UI_FILE);
this.builder.connect_signals (this);
var window = this.builder.get_object ("window") as Window;
/* ANJUTA: Widgets initialization for gtk_httpclient.ui - DO NOT REMOVE */
window.show_all ();
stderr.printf ("constructor:\n");
stderr.printf ("this: %p\n", this);
stderr.printf ("builder: %p\n", this.builder);
}
catch (Error e) {
stderr.printf ("Could not load UI: %s\n", e.message);
}
}
[CCode (instance_pos = -1)]
public void on_destroy (Widget window)
{
Gtk.main_quit();
}
public void build_and_send1 (Widget button)
{
stderr.printf ("\nbuild_and_send1:\n");
stderr.printf ("this: %p\n", this);
stderr.printf ("builder: %p\n", this.builder);
}
[CCode (instance_pos = -1)]
public void build_and_send2 (Widget button)
{
stderr.printf ("\nbuild_and_send2:\n");
stderr.printf ("this: %p\n", this);
stderr.printf ("builder: %p\n", this.builder);
}
static int main (string[] args)
{
Gtk.init (ref args);
new Main ();
Gtk.main ();
return 0;
}
}
私はこの出力を持っています:
constructor:
this: 0x1a524a0
builder: 0x1a6a230
build_and_send1:
this: 0x1aa2030
builder: 0x5a4fe823
build_and_send2:
this: 0x1a524a0
Program has been terminated receiving signal 11 (Segmentation fault)
シグナルにMainの同じインスタンスが必要な場合は、その前に[CCode(instance_pos = -1)]を付ける必要があると思います。しかし、builder.connect_signals(this)の目標は何ですか。?
そして、なぜthis.builderはコンストラクターではなくbuild_and_send2でセグメンテーション違反を起こすのですか?