【发布时间】:2018-08-30 15:51:23
【问题描述】:
在我的 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找到完整的源代码
【问题讨论】:
-
有一个关于如何使用 TimeoutSource 的示例:valadoc.org/glib-2.0/GLib.MainLoop.html