Details
-
Bug
-
Resolution: Unresolved
-
P3: Somewhat important
-
None
-
5.2.0, 5.4.1, 5.5.0 Alpha
-
None
-
Qt: 5.2.0
Qt Creator: 3.0.1
OS: Windows 7 Professional (64-bit Operating system)
Description
In a QtQuick application,
- I instantiate a QML object by calling a function in a stateless Javascript library.
- This Item has focus.
- I quit the application by calling Qt.quit()
- I see the following error:
TypeError: Cannot read property of null
- My log statements indicate that the object was destroyed and then the onActiveFocusChanged handler was apparently called on a null object.
- If I don't use the JS library function, but instead use a local function that should do the same thing, I do not see any error when I quit the application.
Content of following files are provided here:
- MyItem.qml: A custom QML object to be created.
- test.js: A Stateless JS library that has the function to simply instantiate the QML object defined in file MyItem.qml.
- main.qml: Instantiate the QML object defined in MyItem.qml by calling the function defined in the JS library, or the locally defined function, depending on the useJSLib flag.
test.js
.pragma library function createChildObject( aContainer ) { var component = Qt.createComponent( "MyItem.qml" ) return component.createObject( aContainer, {} ) }
main.qml
import QtQuick 2.0 import "test.js" as Test Rectangle { id: container width: 360 height: 360 // Quit the application on mouse-click. MouseArea{ anchors.fill: parent; onClicked: Qt.quit(); } // Flag to choose whether to use JS library function, // or the local JS function. Both functions look identical. property bool useJSLib: true // Instantiate the child according to the chosen option. Component.onCompleted: { if( useJSLib ) { Test.createChildObject( container ) } else { createChildObject( container ) } } function createChildObject( aContainer ) { var component = Qt.createComponent( "MyItem.qml" ) return component.createObject( aContainer, {} ) } }
MyItem.qml
import QtQuick 2.0 Item { focus: true onActiveFocusChanged: { console.log( "MyItem: onActiveFocusChanged" ) console.log( "MyItem: Active focus:" + activeFocus ) } Component.onDestruction: { console.log( "MyItem: Component.onDestruction" ) } }