Accessing C++ object in QML
Unsolved
QML and Qt Quick
-
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!
-
Source code will help us to the right answer.
-
You can use Connections
Connections { target: _chessModel //old syntax onInvalidMove: { } onValidMove: { } //new syntax function onInvalidMove(/*params*/) { } function onValidMove(/*params*/) { } }