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

problem opening one instance of every Widget(form) and deleting it ...



  • Hi ~~~
    i have multi forms ... and mainwindow .. with a Menu inside it a lot of buttons .. every button connected with a special form
    i want if i click to a button it opens the needed form only one time (one instance) .. and if i close it it release the memory ... if i open it once again it open ...
    every form i want it to open just one instance ...
    for example
    this would open new instance everytime i clicked and clear memory without problem

    activatesoft* actsoft = new activatesoft();
        actsoft->show();
        actsoft->setAttribute(Qt::WA_DeleteOnClose);
    

    //////mainwindow.h

    class MainWindow : public QMainWindow
    {
        Q_OBJECT
        benefits* benefit=NULL;
    // if i set this as private variable the software crashes if i open the form second time (after one close)
    }
    

    //////mainwindow.cpp

    MainWindow::~MainWindow()
    {
        delete ui;
    //// i tried this too delete benefit but it doesn't work
        benefit->deleteLater();
    }
    
    and this :
     if (!benefit)
        benefit = new benefits();
        benefit->show();
    

    would open just one instance but even if i delete it it doesn't release the used memory

    is there any solution


  • Lifetime Qt Champion

    How did you check that deleteLater() (or a direct delete) does not work?



  • @Christian-Ehrlicher

    the same yes it doesn't work may be my problem was i deleting it on mainwindow destructor .. not benefit one ...
    mainwindow is the parent
    if i put the instance as private variable .. it release the memory once the window closes ... but if i open it once again the software crashes


  • Lifetime Qt Champion

    Please re-read your last post and think about it if anyone except you understands what you're trying to tell us.



  • @Christian-Ehrlicher

    i have multi forms ... and mainwindow .. with a Menu inside it a lot of buttons .. every button connected with a special form
    i want if i click to a button it opens the needed form only one time (one instance) .. and if i close it it release the memory ... if i open it once again it open ...
    every form i want it to open just one instance ...



  • @Proton-Phoenix
    In a word: why don't you just have an instance (not a pointer to) of this widget/form in your main window? No need to new or delete . One instance.. Sounds like just what you want?


  • Lifetime Qt Champion

    Then don't delete it on close and re-open this instance later on - basic c++ stuff, nothing Qt-specific.



  • Thank you so much for helping i will try that
    i still new on c++
    just trying bruce lee methods to learn things as it can be when i hit a barrier .. i search for solution ... until i reach most of its database

    Thank you all of you



  • @JonB it works bro thank you
    problem fixed
    when i tried this before it doesn't open the window at all ... just flashing but now i understand why it's working
    Thank you <3