几秒钟后Vala隐藏Gtk.InfoBar

时间:2018-08-30 15:51:23

标签: glib vala

在我的Vala程序中,当用户单击按钮时,我正在显示Gtk.InfoBar。 现在,我想在几秒钟后自动隐藏Gtk.InfoBar,并将焦点放回到默认的Gtk.Entry。

经过研究后,我认为最好使用GLib.Timeout进行此操作,但是关于此的Valadoc.org-文档不是很有帮助。

我也无法在Internet上找到一些示例。

有人可以告诉我该怎么做吗?

这是我的出处:

namespace Zeiterfassunggtk {
    [GtkTemplate (ui = "/org/gnome/Zeiterfassunggtk/window.ui")]
    public class Window : Gtk.ApplicationWindow {
        [GtkChild]
        Gtk.TreeView treeview1 = new Gtk.TreeView ();
        [GtkChild]
        Gtk.Button refreshbutton;
        [GtkChild]
        Gtk.MenuButton menubutton;
        [GtkChild]
        Gtk.Button menubuttonrefresh;
        [GtkChild]
        Gtk.Button menubuttonsave;
        [GtkChild]
        Gtk.Button menubuttonquit;
        [GtkChild]
        Gtk.InfoBar infobar1;
        [GtkChild]
        Gtk.Label infobar1label;
        [GtkChild]
        Gtk.Entry user_entry;
        [GtkChild]
        Gtk.Entry job_entry;
        [GtkChild]
        Gtk.Button addbutton;

        Gtk.TreeIter iter;
        Gtk.ListStore liststore1 = new Gtk.ListStore (3, typeof (string), typeof (string), typeof (string));

        private void setup_treeview (Gtk.TreeView treeview1) {
            treeview1.set_model (liststore1);

            treeview1.insert_column_with_attributes (-1, "Name", new Gtk.CellRendererText (), "text", 0, null);
            treeview1.insert_column_with_attributes (-1, "Job", new Gtk.CellRendererText (), "text", 1, null);
            treeview1.insert_column_with_attributes (-1, "Time", new Gtk.CellRendererText (), "text", 2, null);

            liststore1.append (out iter);
            liststore1.set (iter, 0, "Gerald", 1, "Job1", 2, "2018-01-01 18:23", -1);
        }

        void refresh () {
            liststore1.append (out iter);
            liststore1.set (iter, 0, "Gerald", 1, "Job1", 2, "2018-01-01 18:23", -1);
            infobar1.set_revealed (true);
            infobar1label.set_label ("Refreshed!");
        }

        void save () {
            liststore1.append (out iter);
            liststore1.set (iter, 0, "Gerald", 1, "Job2", 2, "2018-01-01 24:00", -1);
            user_entry.set_text ("");
            job_entry.set_text ("");
            user_entry.grab_default ();
            infobar1.set_revealed (true);
            infobar1label.set_label ("Saved!");
        }

        void hideinfobar () {
            infobar1.set_revealed (false);
            infobar1label.set_label ("Close");
        }

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

            this.maximize ();

            this.setup_treeview (treeview1);

            // Don't show infobar1 on start
            infobar1.set_revealed (false);

            // Close infobar1 when Esc is hit.
            infobar1.close.connect (this.hideinfobar);

            // Close infobar1 when the close button is clicked.
            infobar1.response.connect (this.hideinfobar);

            refreshbutton.clicked.connect (this.refresh);
            menubuttonrefresh.clicked.connect (this.refresh);
            menubuttonsave.clicked.connect (this.save);
            menubuttonquit.clicked.connect (app.quit);
            addbutton.clicked.connect (this.save);

            job_entry.set_activates_default (true);
            job_entry.activate.connect (this.save);
            user_entry.activate.connect (job_entry.grab_focus_without_selecting);


            this.show_all ();
        }
    }
}

您可以在github.com

上找到完整的源代码

2 个答案:

答案 0 :(得分:2)

在JensMühlenhoff的帮助下,这是一个有效的代码:

namespace Zeiterfassunggtk {
    [GtkTemplate (ui = "/org/gnome/Zeiterfassunggtk/window.ui")]
    public class Window : Gtk.ApplicationWindow {
        [GtkChild]
        Gtk.Button button1;
        [GtkChild]
        Gtk.InfoBar infobar1;
        [GtkChild]
        Gtk.Label infobar1label;

        public void hideinfobar () {
            infobar1.set_revealed (false);
            infobar1label.set_label ("");
        }

        public void showinfobar (string message) {
            infobar1label.set_label (message);
            infobar1.set_revealed (true);

            Timeout.add_seconds (5, () => {
                this.hideinfobar ();
                return false;
            });
        }

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

            // Don't show infobar1 on start
            this.hideinfobar ();

            // Close infobar1 when Esc is hit.
            infobar1.close.connect (this.hideinfobar);

            // Close infobar1 when the close button is clicked.
            infobar1.response.connect (this.hideinfobar);

            // Connect Button and show infobar1
            button1.clicked.connect (() => {
                this.showinfobar ("Message");
            });    

            this.show_all ();
        }
    }
}

答案 1 :(得分:1)

您可以像这样使用GLib.Timeout:

Timeout.add_seconds (5, () => {
    stdout.printf ("Hello from timeout");
    return false;
});

这将在大约5秒钟内打印一条消息。仅在MainLoop已经运行时才起作用(每个Gtk应用程序都是这种情况)。