View Full Version : Best way to drag-drop a QDockWidget on my custom widget

9th July 2018, 03:22

I want the user to be able to drag a QDockWidget onto my custom widget, so that the custom widget then takes ownership of the QDockWidget.

This is would be easy if Qt used Qt's dedicated drag-drop support for their dragging of QDockWidgets, but it seems they dont.
Rather it seems they do a lot of magic ontop of Qt::Hover* events. I gathered this from reading through Qt sourcecode.

Right now, the only feasible way to me seems:

1. Detect if left mouse-key is down and mouse has been hovering over my custom widget for x time.
2. Wait for mouseReleaseEvent if so.
3. Then check if QApplication::focusWidget() is a QDockWidget, and if so, that QDockWidget was drag-dropped on my customwidget.

That seems a bit unsatisfying though. Is there a "cleaner" solution?

9th July 2018, 20:24
All of the support for QDockWidgets is built into QMainWindow - docking, undocking, which areas are allowed, whether they can be nested or collapsed. All that is a lot of code.

So event though QDockWidget is an independent widget derived from QWidget, it is really designed to be used along with QMainWindow or floated on the desktop. You would have a lot of work to duplicate the behavior to allow docking in any arbitrary widget class.

9th July 2018, 20:32
Oh, I dont want to replicate anything. I want to detect the dropping the QDockWidget, nothing else. My custom widget then steals + takes ownership of the QDockWidget::widget, and deletes the now empty QDockWidget container.