Search code examples
qtdrag-and-drop

Drag-n-drop with QWindow


I have custom QWidget that contains custom QWindow. QWindow with OpenGL is used as a "connector" between render framework and Qt application.

Mouse and keyboard events are handled with overriding QWindow methods.

Pseudo-code:

class MyWindow : public QWindow
{
public:
  MyWindow : QWindow() { /* GL stuff init*/ }
protected:
  // mouse/keyboard event handling
  // expose event handling
  // resize event handling
  // ...
};

class MyWidget : public QWidget
{
public:
  MyWidget : QWidget()
  {
    auto window = new MyWindow();
    auto container = createWindowContainer(window);
    layout()->addWidget( container );
    setAcceptDrops( true );
  }

protected:
  // overriding drop event, but is doesn't work
};

Question: how to handle drop events (it doesn't matter where)?

Problems:

  1. QWindow doesn't provide virtual methods for drag-n-drop support.
  2. QWidget::dragEnterEvent, QWidget::dropEvent (and similar) are not called.
  3. QWindow still accept mouse events, even setMouseGrabEnabled( false ); is set.

Note: I found that call of setMouseGrabEnabled( false ); doesn't blocks mouse events handling in QWindow.


Solution

  • I found a solution:

    It is necessary to install event filter on QWindow and process events there (eventFilter).

    It is possible to install event filter on QWidget (container) but it doesn't work on OS X. Probably it is a bug in Qt, because under Win everything is fine.