Events 瓦拉GTK+;。自定义小部件的问题

Events 瓦拉GTK+;。自定义小部件的问题,events,gtk,vala,gtkentry,Events,Gtk,Vala,Gtkentry,我需要创建一个只接受数字的gtk.Entry。但是我不能覆盖继承类中的key\u press\u事件。只有通过连接功能使用原始条目时,它才有效。 我做错了什么 using Gtk; public class NumberEntry : Entry { public void NumberEntry(){ add_events (Gdk.EventMask.KEY_PRESS_MASK); } //With customized event left e

我需要创建一个只接受数字的gtk.Entry。但是我不能覆盖继承类中的key\u press\u事件。只有通过连接功能使用原始条目时,它才有效。 我做错了什么

using Gtk;

public class NumberEntry : Entry {

    public void NumberEntry(){
        add_events (Gdk.EventMask.KEY_PRESS_MASK);
    }
    //With customized event left entry editing is not possible
    public override bool key_press_event (Gdk.EventKey event) {
        string numbers = "0123456789.";
            if (numbers.contains(event.str)){
               return false;
            } else {
               return true;
            }
        }
    }

public class Application : Window {

    public Application () {
        // Window
        this.title = "Entry Issue";
        this.window_position = Gtk.WindowPosition.CENTER;
        this.destroy.connect (Gtk.main_quit);
        this.set_default_size (350, 70);

        Grid grid = new Grid();
        grid.set_row_spacing(8);
        grid.set_column_spacing(8);

        Label label_1 = new Label ("Customized Entry, useless:");
        grid.attach (label_1,0,0,1,1);

        //Customized Entry:
        NumberEntry numberEntry = new NumberEntry ();
        grid.attach(numberEntry, 1, 0, 1, 1);

        Label label_2 = new Label ("Working only numbers Entry:");
        grid.attach (label_2,0,1,1,1);

        //Normal Entry
        Entry entry = new Entry();
        grid.attach(entry, 1, 1, 1, 1);


        this.add(grid);

        //With normal Entry this event works well:
        entry.key_press_event.connect ((event) => {
            string numbers = "0123456789.";
            if (numbers.contains(event.str)){
                return false;
            } else {
                return true;
            }
        });
    }
}

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

    Application app = new Application ();
    app.show_all ();
    Gtk.main ();
    return 0;
}

超类的
key\u press\u事件
不再被调用。您需要调用基类并在使用密钥时返回true

public override bool key_press_event (Gdk.EventKey event) {
    string numbers = "0123456789.";
    if (numbers.contains(event.str)){
       return base.key_press_event (event);
    } else {
       return true;
    }
}
如果在信号中返回false,则可以将其传递给备用处理程序,但前提是使用
connect
且不重写信号方法