Results 1 to 5 of 5

Thread: Properly destroy a qml dynamic object derived from another item

  1. #1
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Properly destroy a qml dynamic object derived from another item

    Hi

    I have a Physics.qml

    Qt Code:
    1. Item {
    2. id: root
    3. .........
    4. signal destroyBullet()
    5. onDestroyBullet: {
    6. console.log("destroy Physics.qml")
    7. root.destroy()
    8. }
    9. ...........
    10. }
    To copy to clipboard, switch view to plain text mode 

    and a Bullet.qml
    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. Physics {
    4. id: root
    5. ................
    6. onDestroyBullet: {
    7. console.log("destroy Bullet.qml")
    8. root.destroy()
    9. }
    10. .........
    11. }
    To copy to clipboard, switch view to plain text mode 

    Bullet is dynamically created, when is destroyed, do I need to call destroy() in both Bullet.qml and Physics.qml ? Or it's enought just to call destroy in Physics.qml, or in Bullet.qml?
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Properly destroy a qml dynamic object derived from another item

    You only need to destroy each object once. The context is not important as long as you call it on appropriate object.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Properly destroy a qml dynamic object derived from another item

    Still a bit confused by your answer. Please note that Physics.qml is just a "template" for Bullet.qml, I'm only creating dynamic Bullet objects.
    To me it's more convenient to call destroy in Physics.qml, because that's where I have all the logic,
    so when you say context is not important you mean that I can destroy it from Physics context and remove the code related to destroy from Bullet.qml, right ?
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Properly destroy a qml dynamic object derived from another item

    QML is like C++ in this regard - the object "id" is similar to a pointer in C++ where if you have access to the pointer you can call any of its public methods including the delete operator (regardless if it is a final class or not). Putting aside validity of the pattern where an object calls destroy() on itself as well as having a destroyBullet() signal in super-type of the bullet type, you can destroy it from within the base type.

    Having said that it is more kosher to call destroy() on the bullet from outside the bullet object itself so that you can use Bullet for various things.

    javascript Code:
    1. Component {
    2. id: bulletComponent
    3. Bullet {
    4. id: bullet
    5. SequentialAnimation on y {
    6. NumberAnimation {
    7. from: screen.height
    8. to: 0
    9. duration: 3000
    10. }
    11. ScriptAction { script: bullet.destroy() } // die after reaching y=0
    12. }
    13. }
    14. }
    15.  
    16. // ...
    17.  
    18. var bullet = bulletComponent.createObject(someParent, { ... })
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    john_god (4th September 2017)

  6. #5
    Join Date
    Aug 2008
    Location
    Algarve, Portugal
    Posts
    288
    Thanks
    23
    Thanked 32 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Properly destroy a qml dynamic object derived from another item

    Thank you.
    __________________________________________________
    My projects: calculator MathGraphica ; SuperEpicMegaHero game ; GooglePlay ; bitbucket ; github
    Like my projects ? Buy me a kofi

Similar Threads

  1. Replies: 6
    Last Post: 23rd March 2013, 22:50
  2. Destroy object
    By Viper666 in forum Qt Programming
    Replies: 6
    Last Post: 5th January 2013, 15:43
  3. Check if a object is null and destroy objects
    By ruben.rodrigues in forum Newbie
    Replies: 3
    Last Post: 2nd July 2010, 11:25
  4. How to destroy an object right
    By Cruz in forum Newbie
    Replies: 7
    Last Post: 22nd January 2009, 20:43
  5. Object Not destroy
    By Sudhanshu Sharma in forum Qt Programming
    Replies: 3
    Last Post: 31st July 2008, 00:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.