Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

programatically change the function associated with a property change



  • Hello,

    I know how to use qt.bindings on a property, but here, I need to programmatically assign a function to a property change.

    I am trying to do:

        onSomeEvent: onVisibleChangedChanged= Qt.binding(function () { something() })
    

    But the qml system does not seem to like this and throws a
    qrc:/qml/main.qml:612: TypeError: Cannot assign to read-only property "visibleChanged"
    error.

    Any guidance on how to handle this?
    Thanks,
    Cyrille



  • @cyrille-de-brebisson said in programatically change the function associated with a property change:

    I need to programmatically assign a function to a property change.

    What could be your use case?
    What is the condition you'll use to go with one function or the other?



  • Hello,

    I need to do something on the property change, but only after the application has fully loaded...
    so I am trying to set this "binding" in the component.onCompleted function...
    Of course, I could add a boolean property to the original component, and do the action onVisibleChanged only when said boolean is true, however, I was trying to use a different method.

    Cyrille



  • This isn't a good idea, but if you really want such behavior, here is some magic:

    ApplicationWindow {
        id: root
        visible: true
        width: 400
        height: 680
    
        property var func: function() {console.log("Hello!")}
    
        Button {
            id: b1
            text: "Test"
        }
    
        Button {
            y: 50
            text: "Magic"
            onClicked: {
                b1.clicked.disconnect(root.func);
                b1.clicked.connect(function(){console.log("Cya!");});
            }
        }
    
        Component.onCompleted: {
            b1.clicked.connect(root.func);
        }
    }
    

    Hovewer, you cannot disconnect signals which are binded to signals (onClicked) for example, I think this is because they sorta anonimous.


Log in to reply