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. Error when accessing C++ property or method from QML
Forum Updated to NodeBB v4.3 + New Features

Error when accessing C++ property or method from QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 315 Views 1 Watching
  • 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
    dalishi
    wrote on last edited by dalishi
    #1

    Hi I follow this tutorial but are not able to access property or method defined in C++ class.

    I have a C++ class Interface defined:

    class Interface : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(QPointF vpose READ getVpose WRITE setVpose NOTIFY vposeChanged)
    
    public:
        explicit Interface(int domainid=0, QObject *parent = nullptr);
        virtual ~Interface() override;
    
        QPointF getVpose() const {
                return vpose;
        }
    
        void setVpose(const QPointF &p) {
                if (p != vpose) {
                    vpose = p;
                    emit vposeChanged();
                }
        }
    
        Q_INVOKABLE void pushCoordinate(const int &index) {
                ...
        }
    
    signals:
        void vposeChanged();
    
    private:
        QPointF vpose; // vehicle pose
     
        int domain_id;
     };
    

    in my main.cpp:

    int main(int argc, char *argv[])
    {
    #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    #endif
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        const QUrl url(QStringLiteral("qrc:/main.qml"));
        QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                         &app, [url](QObject *obj, const QUrl &objUrl) {
            if (!obj && url == objUrl)
                QCoreApplication::exit(-1);
        }, Qt::QueuedConnection);
    
        Interface interface;
        engine.rootContext()->setContextProperty("interface", &interface);
        engine.load(url);
        
        return app.exec();
        
    }
    

    In main.qml:

    ApplicationWindow {
        id: appWindow
        property var map
        property var parameters
        property var fromCoordinate//: QtPositioning.coordinate(59.9483, 10.7695)
        property var toCoordinate//: QtPositioning.coordinate(59.9645, 10.671)
    
        property var testcplusplus: interface.vpose  // Line 13 here QCreator warning: expected token ";"
    

    I got this error when compiling:
    Screenshot from 2022-08-22 12-33-14.png

    It seems QML does not recognize this interface variable from C++. Did I do something wrong?

    J.HilkJ 1 Reply Last reply
    0
    • D dalishi

      Hi I follow this tutorial but are not able to access property or method defined in C++ class.

      I have a C++ class Interface defined:

      class Interface : public QObject
      {
          Q_OBJECT
          Q_PROPERTY(QPointF vpose READ getVpose WRITE setVpose NOTIFY vposeChanged)
      
      public:
          explicit Interface(int domainid=0, QObject *parent = nullptr);
          virtual ~Interface() override;
      
          QPointF getVpose() const {
                  return vpose;
          }
      
          void setVpose(const QPointF &p) {
                  if (p != vpose) {
                      vpose = p;
                      emit vposeChanged();
                  }
          }
      
          Q_INVOKABLE void pushCoordinate(const int &index) {
                  ...
          }
      
      signals:
          void vposeChanged();
      
      private:
          QPointF vpose; // vehicle pose
       
          int domain_id;
       };
      

      in my main.cpp:

      int main(int argc, char *argv[])
      {
      #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
          QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
      #endif
          QGuiApplication app(argc, argv);
      
          QQmlApplicationEngine engine;
          const QUrl url(QStringLiteral("qrc:/main.qml"));
          QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                           &app, [url](QObject *obj, const QUrl &objUrl) {
              if (!obj && url == objUrl)
                  QCoreApplication::exit(-1);
          }, Qt::QueuedConnection);
      
          Interface interface;
          engine.rootContext()->setContextProperty("interface", &interface);
          engine.load(url);
          
          return app.exec();
          
      }
      

      In main.qml:

      ApplicationWindow {
          id: appWindow
          property var map
          property var parameters
          property var fromCoordinate//: QtPositioning.coordinate(59.9483, 10.7695)
          property var toCoordinate//: QtPositioning.coordinate(59.9645, 10.671)
      
          property var testcplusplus: interface.vpose  // Line 13 here QCreator warning: expected token ";"
      

      I got this error when compiling:
      Screenshot from 2022-08-22 12-33-14.png

      It seems QML does not recognize this interface variable from C++. Did I do something wrong?

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

      @dalishi choose an other name, interface seems to be something protected/internally used


      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.

      D 1 Reply Last reply
      2
      • J.HilkJ J.Hilk

        @dalishi choose an other name, interface seems to be something protected/internally used

        D Offline
        D Offline
        dalishi
        wrote on last edited by dalishi
        #3

        @J-Hilk Hi you saved my day, bro. Thanks so much!

        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