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. Accessing C++ object in QML

Accessing C++ object in QML

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 3 Posters 275 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.
  • StefanoDS Offline
    StefanoDS Offline
    StefanoD
    wrote on last edited by
    #1

    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
    0
    • StefanoDS Offline
      StefanoDS Offline
      StefanoD
      wrote on last edited by
      #2

      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
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        Source code will help us to the right answer.

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

        1 Reply Last reply
        0
        • R Offline
          R Offline
          RedEx 0
          wrote on last edited by
          #4

          You can use Connections

          Connections {
              target: _chessModel
          
              //old syntax
              onInvalidMove: {
              }
          
              onValidMove: {
              }
          
             //new syntax
             function onInvalidMove(/*params*/) { }
             function onValidMove(/*params*/) { }
          }
          
          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