QML UnitTest missing C ++ context property

I found this older post that is facing the same problem: How to mock a QML component

Unfortunately, there is no solution. To repeat the problem: I have a QML TestCase that imports a module. But this module relies on the root context property , which is usually added to main.cpp . Since this is TestCase , I have no influence on how QQmlApplicationEngine starts.

How to add a missing context property?

+6
source share
1 answer

You can get a QQmlEngine instance by passing a QML element to C ++, where you can use the qmlEngine method.

So, you can set the context property by calling

 qmlEngine(passedQmlItem)->rootContext()->setContextProperty("propertyName", propertyValue); 
0
source

All Articles