Qt destructor for a private widget

There is an application that processes text commands. I have a Qt widget that is closed by some close * command. The Qt::WA_DeleteOnClose set for this widget, it gets closeEvent, but the destructor for this object is called later (I think at idle). If I have two commands close *; get something; close *; get something; the program crashes because get something is called before the destructor for this widget, so it tries to access data deleted by the close * command. How to force Qt to call destructors? QCoreApplication::processEvents() after closing the command does not help. I had a problem after changing the qt version to 4.7.2 from 4.3.3. There is no multithreading.

Thanks in advance.

added

Here is a sample code.

 test *t = new test(); t->show(); std::cout << "before deleteLater()" << std::endl; t->deleteLater(); std::cout << "after deleteLater()" << std::endl; QCoreApplication::sendPostedEvents(); QCoreApplication::processEvents(); std::cout << "after processEvents()" << std::endl; 

test class derived from QDialog. It prints test() in the constructor and ~test() in the destructor. This code gives the following output

 test() before deleteLater() after deleteLater() after processEvents() ~test() 

According to the Qt documentation, it should delete the object to the last cout, right? Looks like a bug in Qt, does anyone know about this? Any workaround?

I asked a question on the Qt mailing list, but was still awaiting an answer.

Thanks.

another update

This code

 Dialog::~Dialog() { std::cout << "~test()" << std::endl; } int main(int argc, char* argv[]) { QApplication app(argc, argv); Dialog* dlg = new Dialog(); dlg->setAttribute(Qt::WA_DeleteOnClose); dlg->show(); dlg->close(); std::cout << "before sendPostedEvents()" << std::endl; QCoreApplication::sendPostedEvents(); std::cout << "after sendPostedEvents()" << std::endl; return app.exec(); } 

prints this

 before sendPostedEvents() after sendPostedEvents() ~test() 

but as soon as I add the CloseEvent handler and call deleteLater () in this function of the sendPostedEvents handler, it starts deleting pending objects.

 void Dialog::closeEvent(QCloseEvent* ev) { deleteLater(); QWidget::closeEvent(ev); } 

prints this before sendPostedEvents () ~ Test () after sendPostedEvents ()

Can someone explain what the hell is going on there? Is this just a mistake? Can I use this as a workaround?

How it works? Shouldn't Qt call deleteLater () automatically after closeEvent is accepted if the CloseOnDelete attribute is set?

+6
source share
2 answers

Setting Qt::WA_DeleteOnClose means that qt can be deleted anytime after calling close() , because qt uses deleteLater() internally. You can provide deletion with a QObject::destroyed() signal.

+5
source

QCoreApplication :: processEvents explicitly skips deletion when events are closed. You need to pass QEventLoop :: DeferredDeletion to processEvents (). i.e. QCoreApplication :: processEvents (QEventLoop :: DeferredDeletion);

+1
source

Source: https://habr.com/ru/post/895236/


All Articles