2017-07-07 34 views
0

我的代码:如果Loader的sourceComponent试题参考其母公司,和我设置“loader.active =假”,我得到一个错误

import QtQuick 2.6 
import QtQuick.Window 2.2 

Window { 
    visible: true 
    width: 640 
    height: 480 

    Item { 
     id: loaderParent 
     Loader { 
      id: loader 
      active: true 
      sourceComponent: Item { 
       parent: loaderParent 
       x: parent.x 
      } 
     } 
    } 

    Item { 
     focus: true 
     Keys.onPressed: { 
      loader.active = false; 
     } 
    } 
} 

当我按下任意键,我得到这个错误:

qrc:/main.qml:16: TypeError: Cannot read property of null

想到我怀疑这个错误是无害的,我想要一个解释或任何想法的修复/解决方法?

报告了here

回答

0

我发现了一个解决方法:取代parent.x,取loaderParent.x。仍然想知道问题发生的原因。

0

Loader似乎在销毁时将项目父项设置为null。 QML对象不会立即被删除,而是使用deleteLater(),这会使对象在另一个事件循环中保持活动状态。

这会导致重新评估绑定表达式,由于父类现在为null,因此不再可能。我遇到过这种行为的更严重的遭遇described here

避免它的一种简单方法是不使用您已经找到的父属性,或者使用更复杂的绑定表达式,如x: loader.active ? parent.x : someFailsafeValue

通过使用onParentChanged: console.log(parent),您可以验证在加载程序停用时父项确实变为null。

+0

我还对其他答案留了言,但只为那些只看到这个答案的人,这里有一些相关的错误报告:https://bugreports.qt.io/browse/QTBUG-60344,https: //bugreports.qt.io/browse/QTBUG-51995。 – Mitch

相关问题