Search code examples
c++qtdata-bindingqmlqabstractlistmodel

Qt-Qml: Binding checkbox to QAbstractListModel


I am facing a problem while developing my program. I have a QAbstractListModel which contain a boole property, and I want to be able to pass that value to my QML file containing a CheckBox.

I found this question on StackOverflow covering the same problem. I am facing the exact same problem but the question and answer cover the case where the property is sent to the qml through a Q_PROPERTY whereas I use a QAbstractListmodel. I have overridden the setData(), data() and flags() methods and all works fine but I have a button that check/uncheck all my CheckBoxes and if the user directly check one of them that one will not be editable through my check/uncheck button.

So basically, I want to know how to set up a bi-directional binding using a QAbstractListModel instead of a Q_PROPERTY.

Thank you.


Solution

  • Sorry for bothering you. I just found out the answer using my very little brain for more than 5 minutes.

    I just needed to use the dataChanged signal in order to be able to connect correctly my model to the view.

     CheckBox {
            id: myCheck
            onClicked: user.status = checked
            Component.onCompleted: checked = user.status
            Connections {
                target: user
                onDataChanged: myCheck.checked = user.status
            }
        }
    

    Didn't know we could pass an existing signal directly to QML just putting on in front on the signal's name.