Re: [Vala] signals



Victor,

truth is I don't have the code on me right now and I won't have access
to my email during the weekend 'cause I can only use the connexion at
work.

The code (at least the relevant part) looks kinda this (I promise I'll
send the actual one on monday):

class UIBuilder : Object {
  private Gtk.Builder builder
  private string gui = "my_ui.ui"
  ...
  public UIBuilder () {
    builder = new Gtk.Builder ();
  }
  ...
  public add_object (string obj) {
    builder.add_object_from_file(obj, gui); 
/*
not sure that's the proper method but I think you'll get what I mean
*/
  }
  ...
}

class Item : Object {
  private string name;
  ...
  private UIBuilder builder;
  ...
  private Gtk.Button ok_button;
  private Gtk.Button rm_button;
  public Gtk.Box box;
  ...
  public signal void ok_button_clicked (string name);
  public signal void rm_button_clicked (string name);
  ...
  public Item (string n) {
    name = n;

    builder = new UIBuilder();
    builder.add_object("item_box");

    box = builder.get_object("item_box") as Gtk.Box;

    ok_button = builder.get_object("ok_button") as Gtk.Button;
    ok_button.clicked.connect ( () => ok_button_clicked (name));

    rm_button = builder.get_object("ok_button") as Gtk.Button;
    rm_button.clicked.connect ( () => rm_button_clicked (name));
  }
}

class ItemsViewport : Object {
  private string[] names;  
  private UIBuilder builder;
  ...
  private Gtk.Grid grid;

  public ItemsViewport () {
    builder = new UIBuilder();
    builder.add_object ("items_viewport");
    
    grid = builder.get_object("item_grid") as Gtk.Grid;
    ...
    init();
  } 
  
  private void init () {
    Item item;
    foreach (string name in names) {
      item = new Item (name);
      grid.attach (item, ...);
      item.ok_button_clicked.connect (on_ok_button_clicked);
      item.rm_button_clicked.connect (on_ok_button_clicked);
    }
  }
  ...

  private void on_ok_button_clicked (string name) {
    stdout.printf ("OK Button for item %s was clicked.\n", name);
  }

  private void on_rm_button_clicked (string name) {
    stdout.printf ("RM Button for item %s was clicked.\n", name);
  }
  ...
}

Of course those are (mostly) not the actual variable names but is the
general idea of what I am trying to do. I recall trying to use the 'ok'
and 'rm' buttons as public and connecting directly to their own signals
but then valac would not compile.

As I said earlier I promise I'll send the actual code on monday morning.

Best regards and thanks for your concern,

D.H. Bahr

On Fri, 2012-09-07 at 19:07 -0006, Victor wrote:
Could you please paste the code somewhere, so we can try to spot the
errors?

This part grabs my attention:
I tried using vala native signals on the box-ish class emitted when
the 
buttons where clicked an then connect those signals in the grid-ish 
class, but nothing seems to happen. 

From what you wrote there, I guess the button instances are public
fields in the box class. For handling click events, you have two
options: use lambda functions and access local data from there, or use
a single "generic" handler for all the buttons.


1) Lambda:  my_button.clicked.connect ( () => { // do something...
 });


2) Generic method:


// Connect the handler somewhere...
my_button.clicked.connect (on_button_click);


void on_button_click (Gtk.Button button) {
     // You can do ref erence comparison here, like:
     if (button == my_button) {


     }


     // button's parent can be accessed using button.parent ...
}


You could also subclass Gtk.Button and provide your own signal there:


public class MyButton : Gtk.Button {
    public signal void custom_signal ();


    public MyButton () { }


    public override void clicked () {
        custom_signal ();
    }
}




Best Regards.


P.S. I speak Spanish


On vie, sep 7, 2012 at 12:06 , D.H. Bahr <dbahr uci cu> wrote:
Hello there! 

I've being experiencing a bug for a while now and I have no clue how
to 
solve it. The situation is as follows: 

I have a set of classes (most of which handle some GtkWidget build
from 
a Glade XML using a GtkBuilder instance). One of those classes has
a 
GtkGrid and another has a GtkBox with a GtkLabel and a GtkButtonBox
with 
two GtkButtonses. To the Grid-ish instance I add several Box-ish 
instances (if you catch my drift) and what I need is to be able to
tell 
which GtkButton from which box-ish instance is being clicked. 

I tried using vala native signals on the box-ish class emitted when
the 
buttons where clicked an then connect those signals in the grid-ish 
class, but nothing seems to happen. 

I hope I've made my self clear. 

Has anybody done something similar?? 

Best regards, 

D.H. Bahr 



10mo. ANIVERSARIO DE LA CREACION DE LA UNIVERSIDAD DE LAS CIENCIAS
INFORMATICAS... 
CONECTADOS AL FUTURO, CONECTADOS A LA REVOLUCION 

http://www.uci.cu 
http://www.facebook.com/universidad.uci 
http://www.flickr.com/photos/universidad_uci 
_______________________________________________ 
vala-list mailing list 
vala-list gnome org 
https://mail.gnome.org/mailman/listinfo/vala-list 





10mo. ANIVERSARIO DE LA CREACION DE LA UNIVERSIDAD DE LAS CIENCIAS INFORMATICAS...
CONECTADOS AL FUTURO, CONECTADOS A LA REVOLUCION

http://www.uci.cu
http://www.facebook.com/universidad.uci
http://www.flickr.com/photos/universidad_uci



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