如何解决当 Gtk.ComboBox 中的活动项更改时更新 Gtk.Label
我想在 Gtk.ComboBox 中的活动项发生更改时更新标签。
这就是我创建组合框的方式
public class Epoch.CitiesChooser : Gtk.Grid {
string[] cities = {"Guwahati","Paris","London","New York"};
public Gtk.ComboBox city1Box;
public Gtk.ComboBox city2Box;
public Gtk.ComboBox city3Box;
public Gtk.ComboBox city4Box;
enum Column {
CITY
}
construct {
var liststore = new Gtk.ListStore (1,typeof (string));
for (int i = 0; i < cities.length; i++) {
Gtk.TreeIter iter;
liststore.append (out iter);
liststore.set (iter,Column.CITY,cities[i]);
}
city1Box = new Gtk.ComboBox.with_model (liststore);
var cell1 = new Gtk.CellRendererText ();
city1Box.pack_start (cell1,false);
city1Box.set_attributes (cell1,"text",Column.CITY);
city1Box.set_active (0);
var city2Box = new Gtk.ComboBox.with_model (liststore);
var cell2 = new Gtk.CellRendererText ();
city2Box.pack_start (cell2,false);
city2Box.set_attributes (cell2,Column.CITY);
city2Box.set_active (1);
var city3Box = new Gtk.ComboBox.with_model (liststore);
var cell3 = new Gtk.CellRendererText ();
city3Box.pack_start (cell3,false);
city3Box.set_attributes (cell3,Column.CITY);
city3Box.set_active (2);
var city4Box = new Gtk.ComboBox.with_model (liststore);
var cell4 = new Gtk.CellRendererText ();
city4Box.pack_start (cell4,false);
city4Box.set_attributes (cell4,Column.CITY);
city4Box.set_active (3);
this.attach (city1Box,0);
this.attach (city2Box,1);
this.attach (city3Box,2);
this.attach (city4Box,3);
}
}
这就是我创建标签并尝试更新它们的方式:
public class Epoch.LabelsGrid : Object {
public Gtk.Label face1_label;
public Gtk.Label face2_label;
public Gtk.Label face3_label;
public Gtk.Label face4_label;
// public Epoch.CitiesChooser cities_chooser;
construct {
face1_label = new Gtk.Label ("");
face1_label.set_markup ("<span font_desc='Inter 14'><b>Guwahati</b></span>");
face1_label.halign = Gtk.Align.CENTER;
face1_label.hexpand = true;
face1_label.margin_top = 6;
face1_label.set_ellipsize (END);
face1_label.set_max_width_chars (12);
face2_label = new Gtk.Label ("");
face2_label.set_markup ("<span font_desc='Inter 14'><b>Paris</b></span>");
face2_label.halign = Gtk.Align.CENTER;
face2_label.hexpand = true;
face2_label.margin_top = 6;
face2_label.set_ellipsize (END);
face2_label.set_max_width_chars (12);
face3_label = new Gtk.Label ("");
face3_label.set_markup ("<span font_desc='Inter 14'><b>London</b></span>");
face3_label.halign = Gtk.Align.CENTER;
face3_label.hexpand = true;
face3_label.margin_top = 6;
face3_label.set_ellipsize (END);
face3_label.set_max_width_chars (12);
face4_label = new Gtk.Label ("");
face4_label.set_markup ("<span font_desc='Inter 14'><b>New York</b></span>");
face4_label.halign = Gtk.Align.CENTER;
face4_label.hexpand = true;
face4_label.margin_top = 6;
face4_label.set_ellipsize (END);
face4_label.set_max_width_chars (12);
var cities_chooser = new Epoch.CitiesChooser ();
cities_chooser.city1Box.changed.connect (() => {
face1_label.set_markup ("<span font_desc='Inter 14'><b>Paris</b></span>");
stdout.printf ("Signal is working correctly");
});
}
}
应用程序编译时没有任何错误,但当组合框 (city1Box) 中的活动项发生更改时,标签不会更新。
谁能告诉我如何正确更新标签?
我认为与信号有关
解决方法
您要更改哪个 CitiesChooser 的组合框?它不能与您要连接的 CitiesChooser 相同,因为它是一个局部变量,您永远不会对它做任何事情。超出范围将被删除。
您需要连接到您 UI 中的 CitiesChooser 的信号。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。