How to delete object on the heap when another heap object is deleted



  • Hi, I have the code:

    @void Processmethod()
    {
    QDialog *ProcessMessage = new QDialog;
    Ui::DialogProcessMessage *Dialog = new Ui::DialogProcessMessage();
    Dialog->setupUi(ProcessMessage); //polymorphy
    ProcessMessage->setModal(true);
    ProcessMessage->setAttribute(Qt::WA_DeleteOnClose);
    connect(Dialog->pushButtonAbort, SIGNAL(clicked()), &Prozess, SLOT(kill()));
    connect(&Prozess6, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(helper()));
    connect(&Prozess6, SIGNAL(error(QProcess::ProcessError)), this, SLOT(helper()));
    connect(this,SIGNAL(enablePushButton(bool)),Dialog->pushButtonClose, SLOT(setEnabled(bool)));
    Prozessmeldung->setModal(true);
    ProcessMessage->show();

    processmethodONE();
    

    }@

    How can I delete the heap-object Dialog best when the heap-object ProcessMessage is deleted (which is deleted when closed)? Both objects must be created on the heap. Furthermore the class "Ui::DialogProcessMessage" is directly created by the ui-file and therefore any changes in it will be deleted everytime the ui-file is changed. Is there a way to delete heap-object Dialog when the heap-object ProcessMessage is deleted without subclassing? greetings


  • Moderators

    Since you are setting the Ui::DialogProcessMessage Dialog to be parented by ProcessMessage (via setupUi()), then ProcessMessage takes ownership of Dialog and will automatically delete it upon its own destruction. That's the beauty of Qt's QObject parent/child relationships.

    Edit to add:

    However, on a related note, if you wanted to force the deletion of two unrelated QObjects, you could do something like:
    @
    // Assume two unrelated QObjects, object1 and object2

    connect(object1, SIGNAL(destroyed(QObject *)), object2, SLOT(deleteLater()));
    // destroyed() is emitted as object1 is destructed, and would then
    // call object2's deleteLater to force it to be cleaned up immediately
    // afterwards on a subsequent eventLoop cycle.
    @



  • yes ,it's beautiful



  • Great to know that. Thx for the information. Problem solved. Could you tell me where I can find the description of the setupUi() method? I couldn't find any. greetings



  • Check the documentation on using Qt Designer. It explains you all the ways you can use the .ui files, including the setupUi call.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.