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

Cant refresh QML item only after using it.



  • Hey. I have an app where user can bind keyboard buttons. When loading from config file everything works fine (even after modifying button in c++ and emitting changed signal / changing it's Text property [which is used in "Binded: .." from other element) - text in the rectangle (KeyBind qml item) is updated from for example"Binded: X" to "Binded: Y". The problem started here: when I actually use this custom qml item (KeyBind) I cant change it's text in other way without restarting an app - from cpp / from other element in qml.

    Video of the problem: (as You can see only those custom buttons are problematic)
    https://www.youtube.com/watch?v=VvmlI2IRjPk

    Code of this custom KeyBind qml element:
    https://pastebin.com/kPc7gHEx


  • Qt Champions 2018

    This is because you are overwriting your binding in KeyBind.

    I guess you are using it like so:

    KeyBind {
        keyName: currentProfile.keyName
    }
    

    When you change the profile or the keyName of the profile the binding gets updated like you expect but when you want to bind a new key and do Keys.onPressed: { /* ... */ root.keyName = "ALT" }, you are losing the original binding.

    What you should do is add change your activated signal to indicate which key got entered: signal activated(string keyName) and call that in Keys.onPressed : root.activated("ALT").

    Then when using KeyBind you can do that:

    KeyBind {
        keyName: currentProfile.keyName
        onActivated: keyName => currentProfile.keyName = keyName
    }
    


  • Didnt know about breaking bindings yet and I feel like I dont understand the code onActivated well.
    What does => means / how it's named?
    Currently the custom item is defined like this:

            LeftBind {
                id: enabler
                x: 291
                y: 15
                keyName: keyTranslator.getKey(Config.enabler) //Config.enabler is vkey (int), this function converts it to readable string (for example from c++ vkey 0x10 to "ALT")
                onAccepted: {
                    var a = keyTranslator.getCode(keyName); // from string to vkey
                    if(a === 404){
                        askForKey()
                    } else {
                        Config.set_enablerBtn(a)
                    }
                }
            }
    

Log in to reply