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. Switch (and checkboxes) Not Following data model values

Switch (and checkboxes) Not Following data model values

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 523 Views
  • 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.
  • J Offline
    J Offline
    JrDeskJockey
    wrote on last edited by
    #1

    Hello. I am baffled by this. I created a super basic Qt Quick app as an example. I have a basic ListModel, ListView, and a Switch in the delegate. I added "All On" and "All Off" buttons. Clicking these buttons I can loop through the data model and easily flip all Switches to the checked position, whether true or false. The switches flip fine, back and forth. Here's the catch...
    If I go over and manually click one Switch to toggle it on and then back off, suddenly using my "All On" or "All Off" buttons begin ignoring that particular Switch. The others that I haven't touched still follow the actions of the button clicks, but those particular switches that I manually clicked either on or off are suddenly ignored by my javascript loop that edits the data model.

    Any ideas?

    import QtQuick 2.9
    import QtQuick.Controls 1.4
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        ListModel {
            id: boringModel
            ListElement{
                name: "Bob"
                mySwitchEnabled: false
            }
            ListElement{
                name: "Joe"
                mySwitchEnabled: false
            }
            ListElement{
                name: "Billy"
                mySwitchEnabled: false
            }
        }
    
        Column {
            id: btnCol
            x: 0
            y: 0
            height: parent.height
            width: 100
            Button {
                id: btnOn
                anchors.left: parent.left
                text: "All On"
                width: parent.width
                height: 20
                onClicked: {
                    console.log('clicked all on');
                    var myCount = boringModel.rowCount();
                    for (var i = 0; i < myCount; i++) {
                        var aListElement = boringModel.get(i);
                        aListElement.mySwitchEnabled = true;
                    }
                }
            }
            Button {
                id: btnOff
                anchors.left: parent.left
                text: "All Off"
                width: parent.width
                height: 20
                onClicked: {
                    console.log('clicked all off');
                    var myCount = boringModel.rowCount();
                    for (var i = 0; i < myCount; i++) {
                        var aListElement = boringModel.get(i);
                        aListElement.mySwitchEnabled = false;
                    }
                }
            }
        }
    
        ListView {
            id: switchView
            anchors.right: parent.right
            height: parent.height
            width: 200
            model: boringModel
            delegate: Rectangle {
                width: parent.width
                height: 30
                color: "#f2f2f2"
                Text {
                    id: myLabel
                    text: name
                    anchors.fill: parent
                }
                Switch {
                    id: mySwitch
                    anchors.right: parent.right
                    checked: mySwitchEnabled
                    height: parent.height
                    onClicked: {
                        console.log('clicked');
                    }
                }
            }
        }
    }
    
    1 Reply Last reply
    0
    • 6thC6 Offline
      6thC6 Offline
      6thC
      wrote on last edited by
      #2

      Any reason for Controls v1?

      This seems to be working fine here for me... what's the symptoms in the model you are speaking of? Visually everything works as expected / intuitive feel to me.

      I can toggle one an alternate value and then reset using the all's...

      The difference I have is:
      import QtQuick 2.10
      import QtQuick.Controls 2.3
      //import QtQuick.Controls 1.4

      J 1 Reply Last reply
      1
      • 6thC6 6thC

        Any reason for Controls v1?

        This seems to be working fine here for me... what's the symptoms in the model you are speaking of? Visually everything works as expected / intuitive feel to me.

        I can toggle one an alternate value and then reset using the all's...

        The difference I have is:
        import QtQuick 2.10
        import QtQuick.Controls 2.3
        //import QtQuick.Controls 1.4

        J Offline
        J Offline
        JrDeskJockey
        wrote on last edited by
        #3

        @6thC How interesting. I updated to 2.3 and works fine. I guess I'll just chalk it up to a bug in an earlier version. Thanks!

        1 Reply Last reply
        1

        • Login

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