Accessing C++ object in QML
Unsolved
QML and Qt Quick
-
wrote on 1 Aug 2021, 12:42 last edited by
Hi,
I'm trying to access an C++ object in QML by setting a context property via theQQmlApplicationEngine
: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!
-
wrote on 1 Aug 2021, 18:19 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
-
Source code will help us to the right answer.
-
wrote on 2 Aug 2021, 08:59 last edited by
You can use Connections
Connections { target: _chessModel //old syntax onInvalidMove: { } onValidMove: { } //new syntax function onInvalidMove(/*params*/) { } function onValidMove(/*params*/) { } }
1/4