Qml context object is null on app shutdown

Viewed 331

This is my code. I get those errors in the console every time I quit the application. Properties work just fine during execution, but I get this annoying warning every time.

qrc:/search.qml:17: TypeError: Cannot read property 'isConnected' of null
qrc:/search.qml:18: TypeError: Cannot read property 'scanStatus' of null
qrc:/search.qml:19: TypeError: Cannot read property 'device' of null

#main.cpp

Utility u;
view->rootContext()->setContextProperty("utility", &u);
#search.qml

property bool connected : utility.isConnected
property bool scanRunning : utility.scanStatus
property var searchedDevice : utility.device
2 Answers

Make sure your Utility object is not destroyed after your QML engine: create it before the engine if both are created on the stack. No need to check if utility is null with this solution.

You have to verify that utility is not null:

property bool connected : utility ? utility.isConnected : false
property bool scanRunning : utility ? utility.scanStatus : false
property var searchedDevice : utility ? utility.device : null
Related