Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. qmetaobject::invokemethod results in: "value is undefined and could not be converted to an object"

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 2.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    r0ller
    wrote on last edited by
    #1

    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

    R 1 Reply Last reply
    0
    • R r0ller

      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

      R Offline
      R Offline
      r0ller
      wrote on last edited by
      #2

      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'.

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved