Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML Switch.checked not evaluating after manually toggling the switch
Forum Updated to NodeBB v4.3 + New Features

QML Switch.checked not evaluating after manually toggling the switch

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CyanBlob
    wrote on last edited by
    #1

    Hey everyone,
    I'm new to Qt and QML and am having an issue with setting the value of a Switch element to the value of a property from a model.
    Here is the switch:

    BoolField
        {
            property int test: 0
            id: bool4
            title.text: model ? model.property : false
            switch1.checked: model ? model.property : false
            switch1.onClicked: {
                model.property = switch1.checked
            }
            anchors.top: bool3.bottom
        }
    

    And the BoolField type:

    import QtQuick 2.4
    import QtQuick.Layouts 1.0
    import QtQuick.Controls 1.3
    
    
    Item
    {
        property alias title: title
        property alias switch1: switch1
        height: 30
        width: parent.width
        Text
            {
            id: title
            height: parent.height
            verticalAlignment: Text.AlignVCenter
        }
    
        Switch
            {
            id: switch1
            anchors.right: parent.right
            height: parent.height
        }
    
    }
    

    Note that model is set in the parent.

    My problem is that while the value of "title.text" changes properly when the model changes, the position of "switch1" does not. Adding log statements to the "title.text" and "switch1.checked" statements indicates that once the switch is toggled manually, it no longer re-evaluates that property when model is changed.

    I would appreciated any help that you can give me, because I am definitely stumped here! Thanks!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      CyanBlob
      wrote on last edited by
      #2

      For anyone else who gets stuck on this in the future, I fixed my problem by using Qt.binding(function() {return model ? model.property : false}) inside the onClicked property to re-bind the toggle.
      http://doc.qt.io/qt-5/qtqml-syntax-propertybinding.html

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved