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. [solved] Access to custom CPP object
QtWS25 Last Chance

[solved] Access to custom CPP object

Scheduled Pinned Locked Moved QML and Qt Quick
qmlcppproperty
3 Posts 2 Posters 1.5k 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.
  • H Offline
    H Offline
    helenebro
    wrote on last edited by helenebro
    #1

    Hi,
    I have an application with one part develop with Cpp and one part with QML. The first part enable to get data from database and the QML part display result. I search the best way to get the data. I've tried to use Q_PROPERTY and qmlRegisterType without succeed.
    When I run the app, I see "my object [object Object] undefined" and the "onMyObjectChanged" seems not be activated;

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include "myapp.h"
    #include <QQmlContext>
    #include <QQmlComponent>
    #include "myobject.h"
    
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
        QQmlApplicationEngine engine;
        QQmlContext *context = engine.rootContext();
        MyApp *myApp = new MyApp();
        context->setContextProperty("myLib", myApp);
        qmlRegisterType<MyObject>("Test", 1, 0, "MyCppObject");
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        return app.exec();
    }
    

    myapp.h

    #ifndef MYAPP_H
    #define MYAPP_H
    
    #include <QObject>
    #include "QDebug"
    #include "myobject.h"
    
    class MyApp : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(MyObject* monObjet READ myObject NOTIFY myObjectChanged)
    public:
        explicit MyApp(QObject *parent = 0);
        Q_INVOKABLE void action();
        MyObject* myObject();
    signals:
        void myObjectChanged();
    private :
        MyObject *m_myObject;
    };
    #endif // MYAPP_H@
    

    myapp.cpp

    #include "myapp.h"
    MyApp::MyApp(QObject *parent) : QObject(parent)
    {
    }
    void MyApp::action(){
        m_myObject = new MyObject(this);
        m_myObject->setName("myName");
        emit myObjectChanged();
    }
    MyObject* MyApp::myObject()
    {
        return m_myObject;
    }
    

    myobject.h

    #ifndef MYOBJECT_H
    #define MYOBJECT_H
    #include <QObject>
    class MyObject : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QString name READ getName)
    public:
        explicit MyObject(QObject *parent = 0);
        QString getName() const;
        void setName(const QString &value);
    private :
        QString name;
    };
    #endif // MYOBJECT_H@
    

    myobject.cpp

    #include "myobject.h"
    MyObject::MyObject(QObject *parent) : QObject(parent)
    {
    }
    QString MyObject::getName() const
    {
        return name;
    }
    void MyObject::setName(const QString &value)
    {
        name = value;
    }
    

    and main.qml

    import QtQuick 2.2
    import QtQuick.Window 2.1
    import Test 1.0
    
    Window {
        visible: true
        width: 360
        height: 360
        property variant myObject: MyCppObject
        MouseArea {
            anchors.fill: parent
            onClicked: {
                myLib.action()
                console.log("my object ", MyCppObject, MyCppObject.name)
            }
        }
        Text {
            text: MyCppObject.name
            anchors.centerIn: parent
        }
        onMyObjectChanged: console.log("myObjectChanged ", myObject, myObject.name)
    }
    
    p3c0P 1 Reply Last reply
    0
    • H helenebro

      Hi,
      I have an application with one part develop with Cpp and one part with QML. The first part enable to get data from database and the QML part display result. I search the best way to get the data. I've tried to use Q_PROPERTY and qmlRegisterType without succeed.
      When I run the app, I see "my object [object Object] undefined" and the "onMyObjectChanged" seems not be activated;

      main.cpp

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include "myapp.h"
      #include <QQmlContext>
      #include <QQmlComponent>
      #include "myobject.h"
      
      int main(int argc, char *argv[])
      {
          QGuiApplication app(argc, argv);
          QQmlApplicationEngine engine;
          QQmlContext *context = engine.rootContext();
          MyApp *myApp = new MyApp();
          context->setContextProperty("myLib", myApp);
          qmlRegisterType<MyObject>("Test", 1, 0, "MyCppObject");
          engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
          return app.exec();
      }
      

      myapp.h

      #ifndef MYAPP_H
      #define MYAPP_H
      
      #include <QObject>
      #include "QDebug"
      #include "myobject.h"
      
      class MyApp : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(MyObject* monObjet READ myObject NOTIFY myObjectChanged)
      public:
          explicit MyApp(QObject *parent = 0);
          Q_INVOKABLE void action();
          MyObject* myObject();
      signals:
          void myObjectChanged();
      private :
          MyObject *m_myObject;
      };
      #endif // MYAPP_H@
      

      myapp.cpp

      #include "myapp.h"
      MyApp::MyApp(QObject *parent) : QObject(parent)
      {
      }
      void MyApp::action(){
          m_myObject = new MyObject(this);
          m_myObject->setName("myName");
          emit myObjectChanged();
      }
      MyObject* MyApp::myObject()
      {
          return m_myObject;
      }
      

      myobject.h

      #ifndef MYOBJECT_H
      #define MYOBJECT_H
      #include <QObject>
      class MyObject : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(QString name READ getName)
      public:
          explicit MyObject(QObject *parent = 0);
          QString getName() const;
          void setName(const QString &value);
      private :
          QString name;
      };
      #endif // MYOBJECT_H@
      

      myobject.cpp

      #include "myobject.h"
      MyObject::MyObject(QObject *parent) : QObject(parent)
      {
      }
      QString MyObject::getName() const
      {
          return name;
      }
      void MyObject::setName(const QString &value)
      {
          name = value;
      }
      

      and main.qml

      import QtQuick 2.2
      import QtQuick.Window 2.1
      import Test 1.0
      
      Window {
          visible: true
          width: 360
          height: 360
          property variant myObject: MyCppObject
          MouseArea {
              anchors.fill: parent
              onClicked: {
                  myLib.action()
                  console.log("my object ", MyCppObject, MyCppObject.name)
              }
          }
          Text {
              text: MyCppObject.name
              anchors.centerIn: parent
          }
          onMyObjectChanged: console.log("myObjectChanged ", myObject, myObject.name)
      }
      
      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @helenebro You need to instantiate it. For eg.

      MyCppObject {
         id: cppObj
      }
      property variant myObject: cppObj
      onMyObjectChanged: console.log("myObjectChanged ", myObject, myObject.name)
      
      

      157

      1 Reply Last reply
      0
      • H Offline
        H Offline
        helenebro
        wrote on last edited by
        #3

        Thank you for your answer but I have find my mistake.
        The property should be : property variant myObject: MyLib.monObject

        1 Reply Last reply
        0

        • Login

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