如何使用 Glade 在 Vala 中子类化 GTK IconView

How to subclass GTK IconView in Vala with Glade

当我尝试使用 Glade 在 Vala 中子类化 GTK IconView 时,出现分段错误。这是 Vala 中的错误,还是我只是做错了什么?这是使用 vala 0.42.3。也许这与 IconView 没有 base() 构造函数有关? (参见:

test.vala:

using Gtk;

public class IconViewSubclass : Gtk.IconView {
  public IconViewSubclass() {

  }
}

public static int main(string[] args) {
  Gtk.init(ref args);
  var builder = new Builder.from_file("test.glade");
  var window = builder.get_object("window") as Window;
  var iconViewSubclass = builder.get_object("iconViewSubclass") as IconViewSubclass;
  iconViewSubclass.set_pixbuf_column(0);
  iconViewSubclass.set_text_column(1);

  window.show_all();
  Gtk.main();
  return 0;
}

test.glade:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkListStore" id="store">
    <columns>
      <!-- column-name pixbuf -->
      <column type="GdkPixbuf"/>
      <!-- column-name text -->
      <column type="gchararray"/>
    </columns>
  </object>
  <object class="GtkWindow" id="window">
    <property name="can_focus">False</property>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkIconView" id="iconViewSubclass">
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="margin">6</property>
        <property name="model">store</property>
      </object>
    </child>
  </object>
</interface>
$ valac --pkg gtk+-3.0 test.vala && ./test
Segmentation fault

看来您需要使用 expose_object() 让 Gtk.Builder 知道 IconViewSubclass 存在。这允许在 Builder UI 定义文件中使用子类型。这是一个编译但没有段错误的示例:

test.ui:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.18"/>
  <object class="GtkWindow" id="window">
    <property name="window-position">GTK_WIN_POS_CENTER</property>
    <property name="default-height">250</property>
    <property name="default-width">250</property>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="IconViewSubclass" id="iconViewSubclass">
        <property name="visible">True</property>
        <property name="can_focus">True</property>
        <property name="margin">6</property>
      </object>
    </child>
  </object>
</interface>

test.vala

using Gtk;

public class IconViewSubclass : Gtk.IconView {}

public static int main(string[] args) {
  Gtk.init(ref args);
  var builder = new Builder ();
  builder.expose_object ("IconViewSubclass", new IconViewSubclass ());
  try {
    builder.add_from_file ("test.ui");
  } catch (Error error) {
    print (@"$(error.message)");
  }
  var window = builder.get_object ("window") as Window;
  var iconViewSubclass = (IconViewSubclass)builder.get_object ("iconViewSubclass");
  iconViewSubclass.set_pixbuf_column (0);
  iconViewSubclass.set_text_column (1);

  window.show_all();
  Gtk.main();
  return 0;
}

您可能需要研究使用具有 Vala [GtkTemplate][GtkChild][GtkCallback] 属性的模板。这些属性将告诉 Vala 为您生成样板代码。

AIThomas 的代码运行良好,但是,为了继续在 Glade 中编辑 UI 文件,我不得不添加目录文件,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<glade-catalog name="test" depends="gtk+">
  <glade-widget-classes>
    <glade-widget-class name="IconViewSubclass" generic-name="iconviewsubclass" title="IconViewSubclass" parent="GtkIconView"/>
  </glade-widget-classes>
  <glade-widget-group name="test" title="test">
    <glade-widget-class-ref name="IconViewSubclass"/>
  </glade-widget-group>
</glade-catalog>

我还发现Vala中的subclass名称必须与UIXML中的widget名称完全对应,即在 test.vala 中将 IconViewSubclass 更改为 IconViewSubclass2 会得到:

$ valac --pkg gtk+-3.0 test.vala && ./test
test.ui:14:1 Invalid object type 'IconViewSubclass'Segmentation fault

您还可以使用命名空间作为 class 名称的一部分,即 namespace Foo { public class Bar : Baz } 应该适用于 UI [=29] 中 FooBar 类型的小部件=] 文件.