Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qml context object is null on app shutdown

Tags:

c++

qt

qml

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
like image 508
Nicolò Rancan Avatar asked Oct 19 '25 18:10

Nicolò Rancan


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.

like image 198
GrecKo Avatar answered Oct 21 '25 09:10

GrecKo


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
like image 30
eyllanesc Avatar answered Oct 21 '25 07:10

eyllanesc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!