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

qmetaobject::invokemethod results in: "value is undefined and could not be converted to an object"



  • Hi All,

    I'm trying to combine two examples:

    1. described in the section "Exposing Methods" of the page:
      https://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html

    2. described in the section "Invoking QML Methods" of the page:
      https://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html

    Managed to combine them, the qml function "clickReturn" gets called and everything is fine till I just want to log the incoming msg on the console as it is in the 2nd example. However, when I'd like to set the text property of the text element when the qml function gets called (see qml below), I get the error: "value is undefined and could not be converted to an object". Interestingly enough, the incoming msg still gets logged on the console successfully.

    import QtQuick 2.15
    import QtQuick.Window 2.15

    Window {
    id: mainWindow
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    Text {
        id: text1
        anchors.centerIn: parent
        text: qsTr("Hello World")
        color: "black"
    }
    
    MouseArea {
        objectName: "mousearea"
        anchors.fill: parent
        onClicked: {
            msgBoard.postMessage("Hello from QML");
            //var result = msgBoard.postMessage("Hello from QML")
            //console.log("Result of postMessage():", result)
            //msgBoard.refresh();
        }
        function clickReturn(msg: string){
            console.log(msg);
            mainWindow.text1.text=msg;
        }
    }
    

    }

    Does anyone have any hint?

    Thanks&regards,
    r0ller



  • Ok, found the answer shortly after posting the question: had to add 'property alias aliasText1: text1.text' within the window and use that alias in the function like 'mainwindow.aliasText1=msg'.


Log in to reply