2
votes

STILL UNSOLVED While the fix of my bug works for the Text item it does NOT work for the MouseArea. console.log(key.keyMouseArea) results in 'undefined'. But I can access it using key.children[1] !?!?!

PARTLY SOLVED: My fault, as text was a property of a child and I can access it using key.textField.text.

I pass an QtQuick object to a javascript function of another QtQuick object using the this pointer. In the called function I can see, in the log output, that the passing succeeded (VirtualKey_QMLTYPE_0(0x14a6bf0)). Now, I wish to access the properties of this object but the object is anonymous, or so. How can I convert or cast this parameter into its original type ?

Something like Qt.castToQtQuickObject(pointer, VirtualKey) or similar.

The available createComponent or createObject methods do not what I need as these create new instances.

Some file.qml:

Controller {
  id: controller
}

VirtualKey {
  Text { // didn't think of this child when asking ...
    id: textField
    text: "Hallo"
  }
  MouseArea {
    objectName: "mouse"
    id: keyMouseArea
  }
  Component.onCompleted: {
    controller.registerKey(this) // passing this instance of VirtualKey
  }

}

Controller.qml:

QtObject {
    function registerKey(key) {

        console.log("VKC: registering key " + key)

        // Output: VirtualKey_QMLTYPE_0(0x14a6bf0)

        console.log(key.textField) // works

        console.log(key.keyMouseArea) // undefined !!!

        console.log(key.children[1]) // works

        console.log(key.children[1].objectName) // -> "mouse"

    }
}
1
This should work, please provide a reproducible example - GrecKo
Did you actually try to log key.text? If so, what is the output? From what I know QML&Javascript are very flexible on this matter and you should be able to get the value of the property - Amfasis
I wouldn't base on the JS this. Can you try to set id for the object and so pass it instead? - folibis
Thanks, will provide an example tomorrow if the problem persists. Logging key.txt prints 'undefined'. - Aaron

1 Answers

0
votes

For some reason I had an property alias for the Text item in VirtualKey. That was the reason it worked for textField but not for keyMouseArea.

Adding property alias keyMouseArea: keyMouseArea in VirtualKey solved the issue for me. Now I can use key.keyMouseArea to access also the MouseArea.