CheckBox needs to track external properties

Bug #1494387 reported by Tim Peeters
10
This bug affects 2 people
Affects Status Importance Assigned to Milestone
ubuntu-ui-toolkit (Ubuntu)
Confirmed
High
Unassigned

Bug Description

Often a CheckBox/Switch is used to show the value of a property outside of the CheckBox. For example:

Switch {
    id: hiddenSwitch
    checked: !header.exposed
    onClicked: header.exposed = !header.exposed
}

At first look, this code may seem fine, but clicking the switch will trigger checked = !checked, which breaks the binding, so if header.exposed updates because of events happening outside of the switch, the switch will be out-of-sync.

Perhaps we need to add some properties to the CheckBox:

CheckBox {
  property Object bindTarget
  property string bindProperty
}

and then the behavior of this:
Switch {
    id: exposedSwitch
    bindTarget: header
    bindProperty: "exposed"
}

can do what we want: the switch shows the value of "exposed", and clicking the switch should update the "exposed" value (which in turn will update the state of the switch).

Note that in this case we could not track "!exposed", so if we need that, some more complicated solution is needed.

--- update 2015-09-11 (thanks Christian) ---
It appears to work like this:
Switch {
    id: hiddenSwitch
    checked: header.exposed
    function trigger() { header.exposed = !header.exposed; }
}

so DON'T use the onClicked or onTriggered signal as you would expect, but override the trigger() function. We need to document this.

Revision history for this message
Launchpad Janitor (janitor) wrote :

Status changed to 'Confirmed' because the bug affects multiple users.

Changed in ubuntu-ui-toolkit (Ubuntu):
status: New → Confirmed
Revision history for this message
Cris Dywan (kalikiana) wrote :

I can only make a guess since you're not providing a complete example, but I don't see a problem with my code doing what you're describing. The binding isn't broken, the only problem I can see is that the state gets reversed because the value is changed twice. To achieve the desired result you need to override trigger().

Changed in ubuntu-ui-toolkit (Ubuntu):
status: Confirmed → Incomplete
Revision history for this message
Tim Peeters (tpeeters) wrote :

Christian, you are right. Overriding the trigger() function works for me!

However, we do not have this documented, and if I am not an exception here, many developers may run into this issue, so I keep the bug open, but I will update its description to say that we need documentation.

description: updated
Changed in ubuntu-ui-toolkit (Ubuntu):
status: Incomplete → Confirmed
importance: Undecided → High
Tim Peeters (tpeeters)
description: updated
Revision history for this message
Tim Peeters (tpeeters) wrote :

More people run into problems here: http://pastebin.ubuntu.com/12417204/

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Bug attachments

Remote bug watches

Bug watches keep track of this bug in other bug trackers.