Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to delete object on the heap when another heap object is deleted
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Streight
    wrote on last edited by
    #1

    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

    I`m Streight ;).

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mlong
      wrote on last edited by
      #2

      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.
      @

      Software Engineer
      My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

      1 Reply Last reply
      0
      • Z Offline
        Z Offline
        zjjhxfy1970
        wrote on last edited by
        #3

        yes ,it's beautiful

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Streight
          wrote on last edited by
          #4

          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

          I`m Streight ;).

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved