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

时间:2016-04-09 05:10:49

标签: gtk vala genie

创建简单的ToolButton时,可以像Toolbar example from the Vala guide一样使用clicked.connect接口。向HeaderBar添加按钮的界面类似于该示例中显示的界面。但是,处理点击连接的方式似乎有所不同(或者有些东西我不知道)。

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

以下是代码:

Content.t

open_button.clicked.connect在编译时返回:

[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)

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

只要对行进行注释,代码就会正常工作(您可能希望为openfile函数添加存根)。

由于

更新

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

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

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)

我应该改为:

    def openfile (self:Button)

或者简单地说:

    def openfile (self:ToolButton)

1 个答案:

答案 0 :(得分:1)

您没有在代码中包含单击处理程序,使用此示例存根它可以正常工作:

def openfile ()
    warning ("Button clicked")

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

相关问题