应用程序关闭时 Qml 上下文对象为空

Nic*_*can 4 c++ qt qml

这是我的代码。每次退出应用程序时,我都会在控制台中收到这些错误。属性在执行期间工作得很好,但我每次都会收到这个恼人的警告。

qrc:/search.qml:17: TypeError: 无法读取 null 的属性“isConnected”
qrc:/search.qml:18: TypeError: 无法读取 null 的属性“scanStatus”
qrc:/search.qml:19: TypeError: 无法读取 null 的属性“device”

#main.cpp

Utility u;
view->rootContext()->setContextProperty("utility", &u);
Run Code Online (Sandbox Code Playgroud)
#search.qml

property bool connected : utility.isConnected
property bool scanRunning : utility.scanStatus
property var searchedDevice : utility.device
Run Code Online (Sandbox Code Playgroud)

Gre*_*cKo 6

确保您的Utility对象在 QML 引擎之后不会被销毁:如果两者都是在堆栈上创建的,则在引擎之前创建它。使用此解决方案无需检查是否utility为空。