更新Gtk.Gtk中激活项目时的标签.组合框已更改



当Gtk中的活动项时,我想更新标签。组合框已更改。

这就是我如何创建组合框

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, "text", 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, "text", 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, "text", Column.CITY);

city4box.set_active (3);

this.attach (city1box, 0, 0);
this.attach (city2box, 0, 1);
this.attach (city3box, 0, 2);
this.attach (city4box, 0, 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的信号。

最新更新