我已经有一段时间没有成功解决这个问题了。这是一个练习,其中旋转按钮和比例不断同步,同时勾选一个复选按钮。但是,当检查按钮未激活时,缩放和旋转都可以自由获取任何值。
在之前的一个问题here中,建议使用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()
是否有人建议如何将绑定链接到检查按钮,使其仅在按下时进行同步?我是否应该继续尝试找到解决问题的其他策略?
由于
答案 0 :(得分: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 ()