1

この問題の解決に成功しなかったのは久しぶりです。チェックボタンをチェックしている間、スピンボタンとスケールを常に同期させる練習です。ただし、チェック ボタンがアクティブでない場合は、スケールとスピンの両方が自由に値を取得できます。

ここの前の質問では、bind_property メソッドを使用することが提案されました。これは、スピンとスケールの両方を常に同期させたい場合に実際にうまく機能します。ただし、チェックボタンのアクティビティ(またはその欠如)にリンクする必要がある場合、機能しません。2 回目にチェック ボタンをクリックすると、プログラムがハングします。

これが私が持っている距離です:

[indent=4]
uses
    Gtk

class TestWindow:Window

    _spin:SpinButton
    _scale:Scale
    _check:CheckButton

    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
        _check =  new CheckButton.with_label ( "Sync both scales!" )
        _check.set_active(true)
        _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 ()
        if (_check.active)
            _spin.adjustment.bind_property( "value", _scale.adjustment, "value",BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL )

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

押されている間だけ同期するようにバインディングをチェックボタンにリンクする方法について誰か提案がありますか? 問題を解決するための他の戦略を見つけようとする必要がありますか?

ありがとう

4

1 に答える 1

1

バインドを元に戻すには、メソッドによって返される のunbindメソッドを呼び出す必要があります。Bindingbind_property

    _binding:Binding

    construct ()
        // ...
        _check.toggled.connect( toggled )
        _check.set_active(true)
        // ...

def toggled ()
    // If there was a binding object before
    if (_binding != null)
        // .. unbind it
        _binding.unbind ()
        // and set it to null to indicate "not bound" state
        _binding = null
    // Bind if necessary
    if (_check.active)
        _binding = _spin.adjustment.bind_property( "value", _scale.adjustment, "value",BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL )

value_changed手動で行うには、イベントを使用できます。

    construct ()
        // ...
        _spin.value_changed.connect (spin_value_changed)
        _scale.value_changed.connect (scale_value_changed)
        _check.toggled.connect (check_toggled)
        _check.set_active(true)
        // ...

    def spin_value_changed ()
        // Only sync, when checkbox is set
        if (_check.active)
            // Avoid circle, only set if different
            if (_scale.get_value () != _spin.get_value ())
                _scale.set_value (_spin.get_value())

    def scale_value_changed ()
        // Only sync, when checkbox is set
        if (_check.active)
            // Avoid circle, only set if different
            if (_scale.get_value () != _spin.get_value ())
                _spin.set_value (_scale.get_value())

    def check_toggled ()
        spin_value_changed ()
于 2016-03-12T13:12:21.503 に答える