Helo,
我有以下.ui文件:
<child>
<object class="GtkModelButton" id="button_neworder">
<property name="can_focus">True</property>
<property name="text" translatable="yes">Neuer Auftrag</property>
<property name="action-name">?</property>
<property name="visible">True</property>
</object>
</child>
以及以下应用程序窗口:
public class Window : Gtk.ApplicationWindow {
[GtkChild]
private unowned Gtk.ListBox listbox_jobs;
private unowned Gtk.Button button_neworder;
public Window (Gtk.Application app) {
Object (application: app, title: "Auftragsverwaltung");
this.button_neworder.clicked.connect(this.method);
}
public void method() {
}
}
如何使用GtkModelButton创建点击信号?
代码
this.button_neworder.clicked.connect(this.method);
不起作用。
您需要使用<signal/>
:将信号添加到.ui文件中
...
<child>
<object class="GtkModelButton" id="button_neworder">
<property name="can_focus">True</property>
<property name="text" translatable="yes">Neuer Auftrag</property>
<property name="action-name">?</property>
<property name="visible">True</property>
<signal name="clicked" handler="button_neworder_clicked" />
</object>
</child>
...
并将该方法识别为具有[GtkCallback]
:的回调
[GtkTemplate(ui = "/template/window.ui")]
public class Window : Gtk.ApplicationWindow {
[GtkChild]
private unowned Gtk.ListBox listbox_jobs;
private unowned Gtk.Button button_neworder;
public Window (Gtk.Application app) {
Object (application: app, title: "Auftragsverwaltung");
this.button_neworder.clicked.connect(this.method);
}
[GtkCallback]
public void button_neworder_clicked(Gtk.ModelButton button) {
}
}