在创建一个简单的 ToolButton 时,可以使用 clicked.connect 接口,就像Vala 指南中的工具栏示例一样。将按钮添加到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 函数添加一个存根)。
谢谢
更新
这个问题值得更新,因为错误实际上不在我上面附加的正文中。
错误在于函数的定义。我写:
def openfile (self:Button)
当我应该改为:
def openfile (self:ToolButton)
或者简单地说:
def openfile ()