PDA

View Full Version : QDrag ownership



tuli
19th July 2018, 13:55
I implemented some custom drag+drop in the standard way. Part of the dragging code:


QDrag* drag = new QDrag(this);
drag->exec();
...

Now I am wondering: Who owns the QDrag* object 'drag'?

It seems to me that I do, and I have to do delete the drag object before returning:


QDrag* drag = new QDrag(this);
drag->exec();
...
delete drag;
...

However, in none of the Qt drag+drop examples this is done. They just let the pointer go out of scope and dont bother about it!

(note that the 'this' passed to the constructor is not a 'QObject* parent' but the source of the dragging.)


https://doc.qt.io/qt-5/qtwidgets-draganddrop-fridgemagnets-dragwidget-cpp.html


The documentation doesnt say anything special either.


Who owns it? Do I have to delete it or not?

wysota
20th August 2018, 10:14
I think Qt takes the ownership. You can easily verify it by subclassing QDrag and putting a qDebug() statement in the destructor to see if and when it is invoked.