Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Facing issue with integration of C++ with QML

Facing issue with integration of C++ with QML

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 159 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.
  • M Offline
    M Offline
    ManiRon28
    wrote on last edited by ManiRon28
    #1

    I have started learning about QML and I started with a basic application

    I am facing this issue "Cannot assign to non-existent default property"

    backtest.h

    #ifndef BACKTEST_H
    #define BACKTEST_H
    
    #include <QObject>
    
    class BackTest:public QObject
    {
        Q_OBJECT
        Q_PROPERTY(int iVal READ getIVal WRITE setIVal RESET resetIVal NOTIFY iValChanged FINAL)
    private:
        int iVal;
    
    
    
    public:
        BackTest();
        int getIVal() const;
        void setIVal(int newIVal);
        void resetIVal();
        signals:
            void iValChanged();
    };
    
    #endif // BACKTEST_H
    

    backTest.cpp

    #include "backtest.h"
    
    
    BackTest::BackTest() {}
    
    int BackTest::getIVal() const
    {
        return iVal;
    }
    
    void BackTest::setIVal(int newIVal)
    {
        if (iVal == newIVal)
            return;
        iVal = newIVal;
        Q_EMIT iValChanged();
    }
    
    void BackTest::resetIVal()
    {
        setIVal({}); // TODO: Adapt to use your actual default value
    }
    

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <backtest.h>
    int main(int argc, char *argv[])
    {
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
    
        qmlRegisterType<BackTest>("MyCppClassModule", 1, 0, "BackTest");
    
        const QUrl url(u"qrc:/untitled2/Main.qml"_qs);
        QObject::connect(
            &engine,
            &QQmlApplicationEngine::objectCreationFailed,
            &app,
            []() { QCoreApplication::exit(-1); },
            Qt::QueuedConnection);
        engine.load(url);
    
        return app.exec();
    }
    

    main.qml

    import QtQuick
    import QtQuick.Controls
    import MyCppClassModule 1.0
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
    
        BackTest {
                id: myCppClassInstance
                Component.onCompleted: {
                            myCppClassInstance.iVal = 42;
                        }
                Text {
                           text: "Property value: " + myCppClassInstance.getIVal
                       }
        }
    }
    

    Kindly help me with what I am missing

    sierdzioS 1 Reply Last reply
    0
    • M ManiRon28

      I have started learning about QML and I started with a basic application

      I am facing this issue "Cannot assign to non-existent default property"

      backtest.h

      #ifndef BACKTEST_H
      #define BACKTEST_H
      
      #include <QObject>
      
      class BackTest:public QObject
      {
          Q_OBJECT
          Q_PROPERTY(int iVal READ getIVal WRITE setIVal RESET resetIVal NOTIFY iValChanged FINAL)
      private:
          int iVal;
      
      
      
      public:
          BackTest();
          int getIVal() const;
          void setIVal(int newIVal);
          void resetIVal();
          signals:
              void iValChanged();
      };
      
      #endif // BACKTEST_H
      

      backTest.cpp

      #include "backtest.h"
      
      
      BackTest::BackTest() {}
      
      int BackTest::getIVal() const
      {
          return iVal;
      }
      
      void BackTest::setIVal(int newIVal)
      {
          if (iVal == newIVal)
              return;
          iVal = newIVal;
          Q_EMIT iValChanged();
      }
      
      void BackTest::resetIVal()
      {
          setIVal({}); // TODO: Adapt to use your actual default value
      }
      

      main.cpp

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include <backtest.h>
      int main(int argc, char *argv[])
      {
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
      
          qmlRegisterType<BackTest>("MyCppClassModule", 1, 0, "BackTest");
      
          const QUrl url(u"qrc:/untitled2/Main.qml"_qs);
          QObject::connect(
              &engine,
              &QQmlApplicationEngine::objectCreationFailed,
              &app,
              []() { QCoreApplication::exit(-1); },
              Qt::QueuedConnection);
          engine.load(url);
      
          return app.exec();
      }
      

      main.qml

      import QtQuick
      import QtQuick.Controls
      import MyCppClassModule 1.0
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")
      
          BackTest {
                  id: myCppClassInstance
                  Component.onCompleted: {
                              myCppClassInstance.iVal = 42;
                          }
                  Text {
                             text: "Property value: " + myCppClassInstance.getIVal
                         }
          }
      }
      

      Kindly help me with what I am missing

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @ManiRon28 I think this might be because you are trying to add Text (a visual element) as a child to BackTest which is a pure QObject, no visuals.

      Try:

      BackTest {
        id: myCppClassInstance
        Component.onCompleted: {
          iVal = 42;
        }
      }
      
      Text {
        text: "Property value: " + myCppClassInstance.getIVal
      }
      

      (Z(:^

      M 1 Reply Last reply
      1
      • sierdzioS sierdzio

        @ManiRon28 I think this might be because you are trying to add Text (a visual element) as a child to BackTest which is a pure QObject, no visuals.

        Try:

        BackTest {
          id: myCppClassInstance
          Component.onCompleted: {
            iVal = 42;
          }
        }
        
        Text {
          text: "Property value: " + myCppClassInstance.getIVal
        }
        
        M Offline
        M Offline
        ManiRon28
        wrote on last edited by
        #3

        @sierdzio Thanks it worked

        1 Reply Last reply
        0
        • M ManiRon28 has marked this topic as solved on

        • Login

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