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. TypeError: Property '' of object QObject(0x6cfdf0) is not a function
Forum Updated to NodeBB v4.3 + New Features

TypeError: Property '' of object QObject(0x6cfdf0) is not a function

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 4 Posters 8.7k 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.
  • D Offline
    D Offline
    davidesalvetti
    wrote on last edited by
    #1

    Hi,

    I'm following an example on how to connect qml and c++. ( this is the link of the example chapter Q_INVOKABLE).
    My program is the following:

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include "receiver.h"
    
    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        Receiver rcvr;
        engine.rootContext()->setContextProperty("Receiver", &rcvr);
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    

    Receiver.h

    #include <QObject>
    #include <QDebug>
    class Receiver : public QObject
    {
    public:
        Receiver();
        Q_INVOKABLE bool postMessage(const QString &msg) {
                qDebug() << "Called the C++ method with" << msg;
                return true;
            }
    };
    

    main.qml

    import QtQuick 2.9
    import QtQuick.Controls 2.2
    
    ApplicationWindow {
        id: window
        visible: true
        width: 640
        height: 480
        title: qsTr("Stack")
    
        header: ToolBar {
            contentHeight: toolButton.implicitHeight
    
            ToolButton {
                id: toolButton
                text: /*stackView.depth > 1 ? "\u25C0" :*/ "\u2630"
                font.pixelSize: Qt.application.font.pixelSize * 1.6
                onClicked: {
    //                if (stackView.depth > 1) {
    //                    stackView.pop()
    //                } else {
                        drawer.open()
                    var result = Receiver.postMessage("Hello from QML")
                    console.log("Result of postMessage():", result)
    //                }
    
                }
            }
    
            Label {
                text: stackView.currentItem.title
                anchors.centerIn: parent
            }
        }
    
        Drawer {
            id: drawer
            width: window.width * 0.66
            height: window.height
    
            Column {
                anchors.fill: parent
    
                ItemDelegate {
                    text: qsTr("Page 1")
                    width: parent.width
                    onClicked: {
                        stackView.push("Page1Form.ui.qml")
                        drawer.close()
                    }
                }
                ItemDelegate {
                    text: qsTr("Page 2")
                    width: parent.width
                    onClicked: {
                        stackView.push("Page2Form.ui.qml")
                        drawer.close()
                    }
                }
            }
        }
    
        StackView {
            id: stackView
            initialItem:"Page1Form.ui.qml"//"HomeForm.ui.qml"
            anchors.fill: parent
        }
    }
    

    Now when I click on the toolbar I try to call the function on Receiver.h I get this error:
    "qrc:/main.qml:23: TypeError: Property 'postMessage' of object QObject(0x6cfdf0) is not a function"

    Anybody knows what I'm doing wrong here? Thanks in advance!

    ODБOïO J.HilkJ 2 Replies Last reply
    0
    • D davidesalvetti

      Hi,

      I'm following an example on how to connect qml and c++. ( this is the link of the example chapter Q_INVOKABLE).
      My program is the following:

      main.cpp

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <QQmlContext>
      #include "receiver.h"
      
      int main(int argc, char *argv[])
      {
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
          Receiver rcvr;
          engine.rootContext()->setContextProperty("Receiver", &rcvr);
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
          if (engine.rootObjects().isEmpty())
              return -1;
      
          return app.exec();
      }
      

      Receiver.h

      #include <QObject>
      #include <QDebug>
      class Receiver : public QObject
      {
      public:
          Receiver();
          Q_INVOKABLE bool postMessage(const QString &msg) {
                  qDebug() << "Called the C++ method with" << msg;
                  return true;
              }
      };
      

      main.qml

      import QtQuick 2.9
      import QtQuick.Controls 2.2
      
      ApplicationWindow {
          id: window
          visible: true
          width: 640
          height: 480
          title: qsTr("Stack")
      
          header: ToolBar {
              contentHeight: toolButton.implicitHeight
      
              ToolButton {
                  id: toolButton
                  text: /*stackView.depth > 1 ? "\u25C0" :*/ "\u2630"
                  font.pixelSize: Qt.application.font.pixelSize * 1.6
                  onClicked: {
      //                if (stackView.depth > 1) {
      //                    stackView.pop()
      //                } else {
                          drawer.open()
                      var result = Receiver.postMessage("Hello from QML")
                      console.log("Result of postMessage():", result)
      //                }
      
                  }
              }
      
              Label {
                  text: stackView.currentItem.title
                  anchors.centerIn: parent
              }
          }
      
          Drawer {
              id: drawer
              width: window.width * 0.66
              height: window.height
      
              Column {
                  anchors.fill: parent
      
                  ItemDelegate {
                      text: qsTr("Page 1")
                      width: parent.width
                      onClicked: {
                          stackView.push("Page1Form.ui.qml")
                          drawer.close()
                      }
                  }
                  ItemDelegate {
                      text: qsTr("Page 2")
                      width: parent.width
                      onClicked: {
                          stackView.push("Page2Form.ui.qml")
                          drawer.close()
                      }
                  }
              }
          }
      
          StackView {
              id: stackView
              initialItem:"Page1Form.ui.qml"//"HomeForm.ui.qml"
              anchors.fill: parent
          }
      }
      

      Now when I click on the toolbar I try to call the function on Receiver.h I get this error:
      "qrc:/main.qml:23: TypeError: Property 'postMessage' of object QObject(0x6cfdf0) is not a function"

      Anybody knows what I'm doing wrong here? Thanks in advance!

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      @davidesalvetti hi
      your class needs Q_OBJECT macro

      1 Reply Last reply
      4
      • D davidesalvetti

        Hi,

        I'm following an example on how to connect qml and c++. ( this is the link of the example chapter Q_INVOKABLE).
        My program is the following:

        main.cpp

        #include <QGuiApplication>
        #include <QQmlApplicationEngine>
        #include <QQmlContext>
        #include "receiver.h"
        
        int main(int argc, char *argv[])
        {
            QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        
            QGuiApplication app(argc, argv);
        
            QQmlApplicationEngine engine;
            Receiver rcvr;
            engine.rootContext()->setContextProperty("Receiver", &rcvr);
            engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
            if (engine.rootObjects().isEmpty())
                return -1;
        
            return app.exec();
        }
        

        Receiver.h

        #include <QObject>
        #include <QDebug>
        class Receiver : public QObject
        {
        public:
            Receiver();
            Q_INVOKABLE bool postMessage(const QString &msg) {
                    qDebug() << "Called the C++ method with" << msg;
                    return true;
                }
        };
        

        main.qml

        import QtQuick 2.9
        import QtQuick.Controls 2.2
        
        ApplicationWindow {
            id: window
            visible: true
            width: 640
            height: 480
            title: qsTr("Stack")
        
            header: ToolBar {
                contentHeight: toolButton.implicitHeight
        
                ToolButton {
                    id: toolButton
                    text: /*stackView.depth > 1 ? "\u25C0" :*/ "\u2630"
                    font.pixelSize: Qt.application.font.pixelSize * 1.6
                    onClicked: {
        //                if (stackView.depth > 1) {
        //                    stackView.pop()
        //                } else {
                            drawer.open()
                        var result = Receiver.postMessage("Hello from QML")
                        console.log("Result of postMessage():", result)
        //                }
        
                    }
                }
        
                Label {
                    text: stackView.currentItem.title
                    anchors.centerIn: parent
                }
            }
        
            Drawer {
                id: drawer
                width: window.width * 0.66
                height: window.height
        
                Column {
                    anchors.fill: parent
        
                    ItemDelegate {
                        text: qsTr("Page 1")
                        width: parent.width
                        onClicked: {
                            stackView.push("Page1Form.ui.qml")
                            drawer.close()
                        }
                    }
                    ItemDelegate {
                        text: qsTr("Page 2")
                        width: parent.width
                        onClicked: {
                            stackView.push("Page2Form.ui.qml")
                            drawer.close()
                        }
                    }
                }
            }
        
            StackView {
                id: stackView
                initialItem:"Page1Form.ui.qml"//"HomeForm.ui.qml"
                anchors.fill: parent
            }
        }
        

        Now when I click on the toolbar I try to call the function on Receiver.h I get this error:
        "qrc:/main.qml:23: TypeError: Property 'postMessage' of object QObject(0x6cfdf0) is not a function"

        Anybody knows what I'm doing wrong here? Thanks in advance!

        J.HilkJ Online
        J.HilkJ Online
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @davidesalvetti in addition to what @LeLev said, your constructor is incomplete, you have to properly initialize the base class (QObject in this case)


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        GrecKoG 1 Reply Last reply
        3
        • J.HilkJ J.Hilk

          @davidesalvetti in addition to what @LeLev said, your constructor is incomplete, you have to properly initialize the base class (QObject in this case)

          GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #4

          @J.Hilk eh, not really. It's just that there won't be any QObject parent passed to the QObject constructor.

          J.HilkJ 1 Reply Last reply
          1
          • GrecKoG GrecKo

            @J.Hilk eh, not really. It's just that there won't be any QObject parent passed to the QObject constructor.

            J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by
            #5

            @GrecKo well have to may be to strong of a wording, but one wouldn't want for bad habits to fester, right ;-)


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            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