2016-04-09 14 views
1

在创建简单的ToolButton时,可以使用如Toolbar example from the Vala guide中的clicked.connect接口。向HeaderBar添加按钮的界面与该示例中显示的界面类似。但是,处理点击连接的方式似乎不同(或者我缺少某些东西)。如何使用ToolButton单击的信号(在HeaderBar中)?

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

下面是代码:

[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.connect回报:

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函数添加存根),代码就会工作。

感谢

UPDATE

这个问题值得更新,因为错误实际上不是在我上面附着体。

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

def openfile (self:Button) 

当我应该将所:

def openfile (self:ToolButton) 

或者干脆:

def openfile() 

回答

1

你没有包括在代码中单击处理程序,用这个例子存根它工作得很好:

def openfile() 
    warning ("Button clicked") 

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

相关问题