0

This is a basic example that I need to get to work to use it in my project.

I need someone to help to destroy and rebuild an object on a button click. What I mean is:

//mainwindow.cpp  

{//constructor 
    ui->setupUi(this);  
    /*private*/frame = new QFrame(ui->centralWidget);  
    /*private*/temp = new QPushButton(frame);  
    QPushButton ok = new QPushButton(ui->centralWidget);  
    ok->setGeometry(100,100,50,50);  
    connect(ok, SIGNAL(clicked()), SLOT(des()));  
}

{//slot des()  
    temp->~QPuhsButton();  
    temp = new QPushButton(frame);  
}

See, all I need is temp to be destroyed and rebuilt.

The line temp = new QPushButton(frame); is not working because with or without it, temp disappears from the layout, meaning temp->~QPuhsButton(); is working.

Now, the reason this is bothering me is because this works:

{//constructor 
    ui->setupUi(this);  
    frame = new QFrame(ui->centralWidget);  
    temp = new QPushButton(frame); 
    temp->~QPuhsButton();  
    temp = new QPushButton(frame);  
    /*QPushButton ok = new QPushButton(ui->centralWidget);  
    ok->setGeometry(100,100,50,50);  
    connect(ok, SIGNAL(clicked()), SLOT(des()));*/
}

I tried this last piece of code to see if it was possible to destroy and rebuild the object the way I was trying to with a button being clicked. Turns out it is, this time temp = new QPushButton(frame); is working and the button stays there.

EDIT: thanks for the answer and the comment, but I am sorry because I didn´t realize something before asking the question.

The buttons were being deleted/destroyed, they were just not being "repainted" inside the frame when I write temp = new QPushButton(frame); again, actually they still are not. Help regarding this new problem and again sorry.

Scaramouche
  • 3,188
  • 2
  • 20
  • 46
  • You should **NEVER** call a destructor manually, thats not what they are intended for! (There are exceptions, but your case is not). use `delete temp;` to destroy the object! – Felix Dec 22 '15 at 22:36
  • @Felix,thanx for the tip, please see my update – Scaramouche Dec 22 '15 at 23:33

2 Answers2

1

you can use delete to destroy objects. destroy method can only be called by the object itself. and qwidgets can be automatically recycled by the application. use pointer to point to the new memory space.

zdwchang
  • 11
  • 1
  • upvote for you but please check my edit, the objects were being deleted/destroyed either way(although I am clear now on which is the correct way). – Scaramouche Dec 22 '15 at 23:37
  • 1
    but it works on my QtCreator.. Actually, I dont know what your "repainted" means. you can try these codes to see whether the temp is the temp deleted before.`ui->setupUi(this); QFrame* frame = new QFrame(ui->centralWidget); QPushButton* temp = new QPushButton(frame); temp->setText("temp1"); delete temp; temp = new QPushButton(frame); temp->setText("temp2");`hmm if you mean the button never appears again, try to add a update(). – zdwchang Dec 23 '15 at 06:04
  • setting the text really helped me but using the tip from ddriver `temp->show();` was what I was missing, I did that and that was it, the button shows again, I don't really understand why I have to manually call `show()` if I didn't need to the first time, the first time they appear I just had to create them and passing the frame as parent and that was it, no `show()` required – Scaramouche Dec 23 '15 at 17:07
1

Don't manually call destructors, unless you are using memory pools and placement new.

In Qt it is best to use delateLater() in order to avoid errors from unprocessed events. Also, widgets are by default created hidden, so you need to show() the widget.

So your code should be:

{//slot des()  
    if (temp) temp->deleteLater();
    temp = new QPushButton(frame);  
    temp->show();
}
dtech
  • 47,916
  • 17
  • 112
  • 190
  • in the end you _did_ solve it some how, even though the problem turned out to be other than the one in the title of the answer, `temp->show()` did it, so green tick for u – Scaramouche Dec 23 '15 at 17:09