1

我已经有一段时间没有成功解决这个问题了。这是一个练习,其中一个旋转按钮和一个比例在一个检查按钮被勾选时不断同步。但是,当复选按钮未激活时,刻度和旋转都可以自由获取任何值。

在上一个问题,建议使用 bind_property 方法,当您希望旋转和比例始终同步时,这种方法非常有效。但是,当需要将其链接到检查按钮的活动(或缺少活动)时,它不起作用。第二次单击检查按钮时,程序挂起。

这是我已经走了多远:

[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方法Binding返回的bind_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 回答