如何使用 ToolButton clicked 信号(在 HeaderBar 中)?

How to use the ToolButton clicked signal (in a HeaderBar)?

在创建一个简单的 ToolButton 时,可以使用 clicked.connect 界面,因为 Toolbar example from the Vala guide. The interface of adding a button to a 类似于该示例中显示的界面。但是,处理点击连接的方式似乎有所不同(或者我遗漏了一些东西)。

以下示例是一个小型文本编辑器,其中一个打开的对话框按钮被打包到 HeaderBar 中。 clicked.connection 语法,但是 returns 错误。

代码如下:

[indent=4]
uses
    Gtk

init
    Gtk.init (ref args)

    var app = new Application ()
    app.show_all ()
    Gtk.main ()

// This class holds all the elements from the GUI
class Application : Gtk.Window

    _view:Gtk.TextView

    construct ()

        // Prepare Gtk.Window:
        this.window_position = Gtk.WindowPosition.CENTER
        this.destroy.connect (Gtk.main_quit)
        this.set_default_size (400, 400)


        // Headerbar definition
        headerbar:Gtk.HeaderBar = new Gtk.HeaderBar()
        headerbar.show_close_button = true
        headerbar.set_title("My text editor")

        // Headerbar buttons
        open_button:Gtk.ToolButton = new ToolButton.from_stock(Stock.OPEN)
        open_button.clicked.connect (openfile)

        // Add everything to the toolbar
        headerbar.pack_start (open_button)
        show_all ()
        this.set_titlebar(headerbar)

        // Box:
        box:Gtk.Box = new Gtk.Box (Gtk.Orientation.VERTICAL, 1)
        this.add (box)

        // A ScrolledWindow:
        scrolled:Gtk.ScrolledWindow = new Gtk.ScrolledWindow (null, null)
        box.pack_start (scrolled, true, true, 0)

        // The TextView:
        _view = new Gtk.TextView ()
        _view.set_wrap_mode (Gtk.WrapMode.WORD)
        _view.buffer.text = "Lorem Ipsum"
        scrolled.add (_view)

编译时open_button.clicked.connectreturns:

text_editor-exercise_7_1.gs:134.32-134.39: error: Argument 1: Cannot convert from `Application.openfile' to `Gtk.ToolButton.clicked'
        open_button.clicked.connect (openfile)

当使用 HeaderBar 小部件时,处理该信号的方式是否发生变化?

只要该行被注释,代码就可以工作(您可能想为 openfile 函数添加一个存根)。

谢谢

更新

这个问题值得更新,因为错误实际上不在我上面附上的正文中。

错误在函数的定义处。我写道:

    def openfile (self:Button)

什么时候我应该:

    def openfile (self:ToolButton)

或者简单地说:

    def openfile ()

您没有在您的代码中包含点击处理程序,使用这个示例存根它工作得很好:

def openfile ()
    warning ("Button clicked")

所以我猜你的点击处理程序的类型签名是错误的,这就是编译器在这里抱怨的原因。