Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved Accessing C++ object in QML

    QML and Qt Quick
    3
    4
    87
    Loading More Posts
    • 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.
    • StefanoD
      StefanoD last edited by

      Hi,
      I'm trying to access an C++ object in QML by setting a context property via the QQmlApplicationEngine:

      main.cpp

      ChessModel chessModel;
      QQmlApplicationEngine engine;
      engine.rootContext()->setContextProperty(QStringLiteral("_chessModel"), &chessModel);
      

      Then I try to access it in QML:

      main.qml

      _chessModel.onInvalidMove: invalidLabel.visible = true
          _chessModel.onValidMove: {
              invalidLabel.visible = false;
              console.log("valid move ", col, row);
          }
      

      But I get a runtime error:
      QQmlApplicationEngine failed to load component
      qrc:/main.qml:134:5: Cannot assign to non-existent property "_chessModel"

      Has anyone an idea why this fails?
      My code is publicly available on GitGub.

      Thanks in adanvce!

      1 Reply Last reply Reply Quote 0
      • StefanoD
        StefanoD last edited by

        I fixed some bugs. Now the property squareSize can't be found although it has been declared directly in QML...

        ReferenceError: squareSize is not defined

        1 Reply Last reply Reply Quote 0
        • dheerendra
          dheerendra Qt Champions 2022 last edited by

          Source code will help us to the right answer.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply Reply Quote 0
          • R
            RedEx 0 last edited by

            You can use Connections

            Connections {
                target: _chessModel
            
                //old syntax
                onInvalidMove: {
                }
            
                onValidMove: {
                }
            
               //new syntax
               function onInvalidMove(/*params*/) { }
               function onValidMove(/*params*/) { }
            }
            
            1 Reply Last reply Reply Quote 0
            • First post
              Last post