开发者

How can I get the QObject::deleteLater() to zero the object?

Normally when I delete an object I set it to zero. This helps in that I code my routines to check the object is not zero then proceed.

However if I use the deleteLater() function I have lost control of it. I don't know if its a valid item or if I need to create a new one. The solution I found w开发者_运维问答as just to keep track of the state separately however is there a better way?


There is nothing wrong if you call deleteLater() on your object and then set your pointer to nullptr. Qt framework will safely delete the object for you.

As an alternative you can also connect to destroyed signal to be notified of your object's destruction and set your pointer to nullptr in that slot.


You can use QPointer if the object that is being deleted is a QObject. This will set it to NULL.


Do you mean, "zero the pointer"? In C++ it's more important than in Java to make the distinction between the object and a pointer to it. Here, it's especially important, because if you zero the object its destructor won't work. If you zero a pointer to the object, no problem, because Qt will have added another pointer to the "objects-to-be-deleted" list.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜