Vala GtkButton.Clicked.Connect没有调用函数

问题描述 投票:1回答:1

我还在努力学习vala,我遇到了GtkButton信号的问题。

我想将函数void refresh ()连接到GtkButton。单击Button时,应调用该函数并设置GtkLabel的Label。所以我写GtkButton.clicked.connect (this.function);。这应该在我点击按钮时调用该功能,对吗?

我的功能非常简单,可以更改GtkLabel的文本。所以我得到了void function () { GtkLabel.label = "New Text"; }

当我测试这个小程序时,点击按钮什么都不做,或者至少我看不到任何东西。

我错过了什么?

这是我的代码:

namespace Zeiterfassunggtk {
    [GtkTemplate (ui = "/org/gnome/Zeiterfassunggtk/window.ui")]
    public class Window : Gtk.ApplicationWindow {
        [GtkChild]
        Gtk.Button refreshbutton;
        Gtk.Button menubuttonrefresh;

        void refresh () {
            label1.label = "Clicked";
        }

        public Window (Gtk.Application app) {
            Object (application: app);

            refreshbutton.clicked.connect (this.refresh);
            menubuttonrefresh.clicked.connect (this.refresh);

            this.show_all ();
        }
    }
}

你可以看看github.com的完整代码

gtk3 vala
1个回答
1
投票

如果它们在模板中,则每个字段都需要[GtkChild]。现在,menurefresh包含null,不会连接任何东西。 label1也是null,因此更改其标签将不会做任何事情。

正确的代码是:

namespace Zeiterfassunggtk {
    [GtkTemplate (ui = "/org/gnome/Zeiterfassunggtk/window.ui")]
    public class Window : Gtk.ApplicationWindow {
        [GtkChild]
        Gtk.Button refreshbutton;
        [GtkChild]
        Gtk.Button menubuttonrefresh;
        [GtkChild]
        Gtk.Label label1;

        void refresh () {
            label1.label = "Clicked";
        }

        public Window (Gtk.Application app) {
            Object (application: app);

            refreshbutton.clicked.connect (this.refresh);
            menubuttonrefresh.clicked.connect (this.refresh);

            this.show_all ();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.