0

私の目的は、スケールのスライダーをスピンの値と同期させ、その逆にすることです。いくつかの代替手段を試しましたが、新しいスピン位置を設定できるようにスケールの値を取得する方法が見つかりません。

小さな演習には、両方のウィジェットを同期する小さなチェック ボタンがあります。トグル機能を使用してこの同期を実行しようとしていますが、現在行き詰まっています。

これまでのコードは次のとおりです。

uses
    Gtk

class TestWindow:Window
    _spin:Gtk.SpinButton
    _scale:Gtk.Scale
    construct ()

        //General aspects of the window
        title = "Spin and scale"
        window_position = WindowPosition.CENTER
        destroy.connect( Gtk.main_quit )

        //create the spin button
        spinAdjustment:Gtk.Adjustment = new Gtk.Adjustment(50, 0, 100, 1, 5, 0)
        scaleAdjustment:Gtk.Adjustment = new Gtk.Adjustment(50, 0, 100, 1, 5, 0)
        _spin:SpinButton = new Gtk.SpinButton(spinAdjustment, 1.0,1)

        //create the horizontal scale
        _scale:Scale = new Gtk.Scale(Gtk.Orientation.HORIZONTAL, scaleAdjustment);

        //create the check button
        var check =  new Gtk.CheckButton.with_label ("Sync both scales!")
        check.toggled.connect(toggled)

        // organize it in a box
        var box = new Box( Orientation.VERTICAL, 0 )
        box.pack_start(_spin, true, true, 0 )
        box.pack_start(_scale, true, true, 0)
        box.pack_start(check, true, true, 0)
        add( box )

    def toggled ()
        message(_spin.get_value().to_string())
        //if _scale.get_value_pos() !=  _spin.get_value()
        //  _scale.set_value_pos(_spin.get_value())



init
    Gtk.init( ref args )
    var test = new TestWindow()
    test.show_all()
    Gtk.main()

これを解決する方法について、誰かが私に少しの指針を与えることができますか?

4

1 に答える 1

2

これは、コードに基づいた実際の例です。

[indent=4]
uses
    Gtk

class TestWindow:Window

    _spin:SpinButton
    _scale:Scale

    construct ()
        //General aspects of the window
        title = "Spin and scale"
        window_position = WindowPosition.CENTER
        destroy.connect( main_quit )

        //create the spin button
        spinAdjustment:Adjustment = new Adjustment( 50, 0, 100, 1, 5, 0 )
        scaleAdjustment:Adjustment = new Adjustment( 50, 0, 100, 1, 5, 0 )
        _spin = new SpinButton( spinAdjustment, 1.0, 1 )

        //create the horizontal scale
        _scale = new Scale( Orientation.HORIZONTAL, scaleAdjustment );

        //create the check button
        var check =  new CheckButton.with_label ( "Sync both scales!" )
        check.toggled.connect( toggled )

        // organize it in a box
        var box = new Box( Orientation.VERTICAL, 0 )
        box.pack_start( _spin, true, true, 0 )
        box.pack_start( _scale, true, true, 0 )
        box.pack_start( check, true, true, 0 )
        add( box )

    def toggled ()
        message(_spin.get_value().to_string())
        if _scale.get_value() !=  _spin.get_value()
            _scale.set_value(_spin.get_value())

init
    Gtk.init( ref args )
    var test = new TestWindow()
    test.show_all()
    Gtk.main()

あなたの例は素晴らしく、明らかにそれを実行すると、ユーザーの操作がどのように改善されるかを理解できます。

Genie コード自体には、いくつかのポイントがあります。

  • 変数識別子とその型を指定すると、これは変数の新しい宣言になります。多くの場合、Vala コンパイラは警告しますが、同じ名前の変数がクラスのメソッドとクラス全体のスコープで宣言されている場合は警告しません。すでに宣言しているから_spin:SpinButton = new Gtk.SpinButton(spinAdjustment, 1.0,1)です。についても同様です。_spin = new Gtk.SpinButton(spinAdjustment, 1.0,1)_spin_scale
  • get_value_posのメソッドを使用したGtk.Scaleため、型の不一致エラーが発生しました: Equality operation: 'double' and 'Gtk.PositionType' are incompatibleGtk.Scaleの Vala ドキュメントには、「これを使用するには、おそらく、GtkScale 自体のメソッドに加えて、その基本クラスである Range のメソッドを調査する必要があるでしょう」と記載されています。その下Gtk.Rangeには、を返すget_valueメソッドがありdoubleます。これは継承の概念であり、すでにget_valueSpinButtonWindow
于 2016-02-21T18:44:28.440 に答える