1

使用 vala 和 glade ui,我没有在 glade 中分配信号签名。但是,我确实在 glade 中创建了一个名为“new_action”的 Gtk.Action 对象:

<object class="GtkAction" id="new_action">
    <property name="label" translatable="yes">New</property>
    <property name="short_label" translatable="yes">New</property>
    <property name="tooltip" translatable="yes">Create a new DESKTOP file</property>
    <property name="stock_id">gtk-new</property>
    <property name="is_important">True</property>
</object>

稍后,这里是使用“new_action”的按钮:

<object class="GtkToolButton" id="new_toolbutton">
    <property name="related_action">new_action</property>
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="related_action">new_action</property>
    <property name="label" translatable="yes">New</property>
    <property name="use_underline">True</property>
</object>

在代码中,我首先得到了 Gtk.Builder:

Gtk.Builder builder.add_from_file ("azoth.ui");

然后我创建我的操作对象并分配 lambda 函数:

this.connect_components (); /* my code - shown below in this question */
this.builder.connect_signals (this);

然后在 vala(使用 Gtk.Builder)中,我从构建器中分配了一个 var new_action:

this.new_action = this.builder.get_object ("new_action") as Gtk.Action;

然后我在代码中为它创建了一个 lambda:

this.new_action.activate.connect (() => {
   stdout.printf ("You activated the NEW action!\n");
}

运行时,我单击“新建”按钮,没有输出到标准输出。我觉得我可能错过了一个步骤,因为这个概念很简单,应该很容易在代码中生成动作回调,而不必在 glade 中定义回调。

4

0 回答 0