0

I have a property of type Component.

import QtQuick 2.6

Item {
    id: root
    property Component sourceComponent
    /* ........ */
    Loader {
        anchors.fill: parent
        id: myLoader
    }
    SequentialAnimation {
        id: hideLoader
        NumberAnimation {
            target: root
            property: "y"
            duration: 1000; from: 0; to: -root.height
        }
        onStopped: root.sourceComponent = undefined;  //<-- Error: Cannot assign [undefined] to QQmlComponent*
    }
}

Whenever I want to set it to undefined it raises an error. How can I set it to undefined?
UPDATE
As you can see in my sample code, I want to assign sourceComponent of the loader after finishing the animation. So I need property to be normal not alias.

S.M.Mousavi
  • 5,013
  • 7
  • 44
  • 59
  • Why do you want to assign undefined? – eyllanesc Jul 24 '17 at 04:36
  • 2
    Try assigning null. – eyllanesc Jul 24 '17 at 04:36
  • You could set `myLoader.active = false` if you want to unload the loader. – GrecKo Jul 24 '17 at 07:38
  • @eyllanesc because I want to unload loader from out side same way as Loader – S.M.Mousavi Jul 24 '17 at 10:50
  • @eyllanesc Loader needs to be set as `undefined`. assigning `null` will not causes unload. – S.M.Mousavi Jul 24 '17 at 10:52
  • @eyllanesc setting it to `null` seems to works but is it OK? the documentation says to set `sourceComponent` to `undefined` to destroy current item, freeing resources, leaving Loader empty. – S.M.Mousavi Jul 24 '17 at 11:11
  • @GrecKo the documentation says to set `sourceComponent` to `undefined` to destroy current item, freeing resources, leaving Loader empty. So is it enough to setting `active` property `false`? is it same affect as setting `sourceComponent` to `undefined`? – S.M.Mousavi Jul 24 '17 at 11:15
  • @S.M.Mousavi the documention of `active` says "Setting the value to inactive will cause any item loaded by the loader to be released, but will not affect the source or sourceComponent.", so yes. – GrecKo Jul 24 '17 at 12:01

2 Answers2

1

Loader documentation says:

If the source or sourceComponent changes, any previously instantiated items are destroyed. Setting source to an empty string or setting sourceComponent to undefined destroys the currently loaded object, freeing resources and leaving the Loader empty.

So, you can simply set its source property an empty string.

In your case, you would need an alias property called sourceComponent, not a normal property.

property alias sourceComponent: myLoader.sourceComponent 
frogatto
  • 28,539
  • 11
  • 83
  • 129
  • Thank you. You can see an animation in my sample code. I want to assign `sourceComponent` of the `loader` after completion of the animation. So I need to get it as a normal property and affect it at a proper time. – S.M.Mousavi Jul 24 '17 at 11:20
  • @S.M.Mousavi That animation only operates on `y` property of `root`, can't see anything related to `sourceComponent` of `root`. – frogatto Jul 24 '17 at 11:41
  • you can see last line of the `hideLoader` animation which handles `onStopped` signal. I will run this animation in some places. Also I want to implement and custom component (above code as a sample) which could handle any value as a `sourceComponent` as a regular Loader one. – S.M.Mousavi Jul 24 '17 at 14:16
  • @S.M.Mousavi Please note that by unsetting `root.sourceComponent` you're not unsetting that of that Loader, so this may cause an unexpected behavior. On the other hand I can't see something wrong with `alias` property. That way, unsetting `root.sourceComponent` will unset `mylaoder.sourceComponent` too. But yours wont. – frogatto Jul 25 '17 at 04:18
0

You can use dynamic object creation to achieve this.

property Component sourceComponent

Instead of defining sourceComponent as Component, define it as a var. Example :

property var sourceComponent = Qt.createComponent("YourCustomImplementation.qml");

Assign this sourceComponent to your Loader sourceComponent.

myLoader.sourceComponent = root.sourceComponent
Praveen Kumar
  • 483
  • 1
  • 5
  • 12