シンプルなツールボタンを作成する際、Vala ガイドのツールバーの例のように、clicked.connect インターフェイスを使用できます。ボタンを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 ()