Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Delete all instances of an object?



  • The way I control different pieces of my program (QFrame's, QPushButtons, etc) is usually by defining them inside mainwindow.h because I often have three or four different functions that change or grab information from one button, frame, etc. I do not want memory leaks and create multiple instances of the same button, therefore I use "btn->deleteLater();" The issue with this is that I do not always know if a button still exists when I am about to recreate that button, another function may have deleted it. Calling this function causes an instant crash of the program flow. Is there a way to delete all instances of an object, without knowing if any instances even exist? Do I need to create vectors for all buttons or what?


  • Lifetime Qt Champion

    Hi
    If you assign it a parent, that parent will delete them when its deleted so often manually deletion is not needed.
    like
    QPushButton *n= new QPushButton(this); // this being the parent/containing widget.

    Have a look here
    https://doc.qt.io/qt-5/objecttrees.html


  • Lifetime Qt Champion

    Hi,

    There's something not clear. Why are you deleting all these widgets if you still need to use them ?



  • @mrjj the parent is basically constant

    @SGaist I delete them because some buttons, frames, etc are used but then as the program continues and the user use the program they are not anymore. These buttons could, for example, be "create an item" button. It is necessary ONLY when creating an item for a list, not when reviewing the list, editing the list, moving things around in the list, searching, and so on. Then I do not want it to take up space nor be visible.

    Previously I just set the object to hidden and a new one was created when I needed it, the issue here however, as I understand it, is that this is a memory leak. I have all these "hidden" object in the background but they are never de-allocated. This is not what I want. Therefore I want to delete the items in the background but if I attempt to delete an item I have already deleted the program crashes, which is not what I want. If I delete an object but there are two instances of it then there is one left, possibly stacking up in the background.



  • @adamsmith said in Delete all instances of an object?:

    Previously I just set the object to hidden and a new one was created

    Why. Just show the old item again instead of creating a new one



  • @VRonin I do not know if the old item has been created yet!
    Example: I have a button that says "Create an item" that leads me to a menu that has a QLineEdit to enter item and a QPushButton that has the action "create item". I cannot set them to "btn_create_item->show();" because if the item does not yet exist then the program crashes. I have to declare a new QPushButton and a new QLineEdit.
    I cannot check if "btn_create_item->isHidden();" because that would be calling to an object that has not yet been created. Then I have to have a whole file full of booleans containing info if a certain button has been created or not. After it has been created I can but I cannot change, modify, move, and so on any of the items in that menu since I do not know if the program just stared and they have not entered the menu that creates the QPushButton or not. I do not want to have a file full of "boolean exist_btn_create_item = false;" and have that for about 50 different items, that is just ugly and bad programming IMO.



  • Normally all the items are created in the constructor so you are sure all of them are created and then hide those that you don't need. An alternative is just to set the default value of the pointers to nullptr so that you can check if an item is created or not just by checking the pointer.

    I must say, however, that, by your description of the UI, your design is a ticking time bomb and you should seriously consider just switching to the traditional and much more manageable "tree" design.



  • @VRonin If I have a main menu where I can either go into "view item list", "edit item list", "search item list" and "create an item". I can access any of the other "menu's" from anywhere inside the program using a drop-down menu, therefore I cannot know in which order I enter the different "menu's". What would be a good way to handle this?



  • You need to add extra logic in your prog.

    Your problem seems very similar to the Copy/Cut/Paste logic. You don't know if the paste command is valid because you don't know if the user press copy command before that.
    Well, is up to you to register the states of this commands.

    So you need to implement the logic suitable to your case, for example, if the list of items is empty, obviously you cannot edit it, etc.

    About the interfaces (as others said), create them on the fly could be tricky and error prone. You can do it if only minor changes are to be made (hide/disable a few button/options)
    If the interfaces are very different, better way to create a unique persistent widget for each, and put them in a StackedWidget.

    Good luck :;)