April 18, 2012

Streight Streight
Lab Rat
26 posts

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

 

Hi, I have the code:

  1. void Processmethod()
  2. {
  3.     QDialog *ProcessMessage = new QDialog;      
  4.     Ui::DialogProcessMessage *Dialog = new Ui::DialogProcessMessage();              
  5.     Dialog->setupUi(ProcessMessage);              //polymorphy
  6.     ProcessMessage->setModal(true);
  7.     ProcessMessage->setAttribute(Qt::WA_DeleteOnClose);
  8.     connect(Dialog->pushButtonAbort, SIGNAL(clicked()), &Prozess, SLOT(kill()));  
  9.     connect(&Prozess6, SIGNAL(finished(int, QProcess::ExitStatus)),  this, SLOT(helper()));
  10.     connect(&Prozess6, SIGNAL(error(QProcess::ProcessError)),  this, SLOT(helper()));
  11.  connect(this,SIGNAL(enablePushButton(bool)),Dialog->pushButtonClose, SLOT(setEnabled(bool)));
  12.     Prozessmeldung->setModal(true);
  13.     ProcessMessage->show();
  14.  
  15.     processmethodONE();
  16. }

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

 Signature 

I`m Streight ;).

4 replies

April 18, 2012

mlong mlong
Mad Scientist
1517 posts

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:

  1. // Assume two unrelated QObjects, object1 and object2
  2.  
  3. connect(object1, SIGNAL(destroyed(QObject *)), object2, SLOT(deleteLater()));
  4. // destroyed() is emitted as object1 is destructed, and would then
  5. //   call object2's deleteLater to force it to be cleaned up immediately
  6. //   afterwards on a subsequent eventLoop cycle.

 Signature 

Senior Software Engineer
AccuWeather Enterprise Solutions
/* My views and opinions do not necessarily reflect those of my employer.  Void where prohibited. */

April 18, 2012

zjjhxfy1970 zjjhxfy1970
Lab Rat
1 posts

yes ,it’s beautiful

April 21, 2012

Streight Streight
Lab Rat
26 posts

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

 Signature 

I`m Streight ;).

April 21, 2012

Andre Andre
Area 51 Engineer
6031 posts

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

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

 
  ‹‹ How can I set the interval for mouse press detection ?      (Solved) Focus in table widget ››

You must log in to post a reply. Not a member yet? Register here!