[Vala] Question about access to instance data in callback methods in same class



Hi,

Sorry to bother again, but i am stuck. I have read the stuff about
access to user_data in callback methods connected thru signals here:
https://live.gnome.org/Vala/SignalsAndCallbacks

However, I cannot get it to work in my project. Again, here is my test code:

using Gtk;
using Goo;
using Gdk;

class Bar {

    private int foo = 1;

        public Bar (Canvas canvas) {

                CanvasItem root;

                root = canvas.get_root_item();

                var rect = CanvasRect.create(root,10,10,100,100);
                rect.fill_color = "black";

                rect.button_press_event.connect(on_button_press);
        }

    private bool on_button_press(CanvasItem Item, Gdk.EventButton EventButton) {
        stdout.printf("%d\n", this.foo++);
            return true;
    }
}


class TestCase : Gtk.Window
{
    public TestCase ()
    {
        this.title = "testcase";
        this.destroy.connect (Gtk.main_quit);
        set_default_size (600, 600);
                                
                var canvas = new Canvas();
                var bar = new Bar(canvas);
                
            add (canvas);
    }

static int main (string[] args)
    {
        Gtk.init (ref args);

        var testcase = new TestCase ();
        testcase.show_all();

        Gtk.main ();

        return 0;
    }
}

It should output a number starting with 1 and increasing by 1 every
time you press a mouse button on the black rectangle, but it either
segvaults or start with a random other number.

Any help would be great!

Greetz,
Fred



2011/7/26 Fred van Zwieten <fvzwieten gmail com>:
Hi Luca,

Yeah, i can imagine. I just pasted the code I had :-)

Never mind. Thanks for trying anyway. I have created a workaround. I
still find it weird it is not possible. I have created a smaller test
case without all the gdk and canvas stuff and it just works.

Greetz,
Fred



2011/7/26 Luca Bruno <lethalman88 gmail com>:
On Tue, Jul 26, 2011 at 11:26:49AM +0200, Fred van Zwieten wrote:
Hi Luca,

Have you had any change to look into my problem? I am sure it is
something obvious, but I just don't see it.

I tried but the test case is too large.

--
http://www.debian.org - The Universal Operating System





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]