Propogate drag events to parent widget
-
Hi everyone,
I want to ask if there is way to propagate dragMoveEvents to parent widget? To receive any drag move events, you need to accpt event in dragEnterEvent. But if you accept this event in child widget, parent will not receive it. So how can I have similar flow like mouseMoveEvent and other, when at the end of these events you just propagate it to QWidget::mouseMoveEvent?
I see only one option with eventFilter for child widget, but are there any other alternatives? -
Hi,
Can you explain your use case ? It's not really usual to have several widgets concerned by the same dragMoveEvent.
That said, I think you are looking for the acceptProposedAction. -
Hi @SGaist, thanks for quick reply
I have QScrollArea widget with list of custom widgets. I start drag in customWidget, by pressing mouse with ctrl. I want to implement custom AutoScrollArea, which make scroll when mouse is near top or bottom border. So I have dragndrop in that customWidgets and just want to receive dragMoveEvent in AutoScrollArea to change scroll bar position.
I do not look at proposedAction, because not understand its idea. For example, I can set two action, like copy and move and in child widget accept only copy action, but move in AutoScrollArea? -
So I made it by eventFilter for every customWidget, setting by AutoScrollArea, but maybe there is a better and simpler solution
Methods declaration:
bool eventFilter(QObject *watched, QEvent *event) override; void onDragEnterEvent(const QDragEnterEvent *event); void onDragMoveEvent(const QPoint &mousePos); void onDragLeaveEvent(); void onDropEvent();
EventFilter:
bool AutoScrollArea::eventFilter(QObject *watched, QEvent *event) { if (event->type() == QEvent::DragEnter) { QDragEnterEvent *dragEvent = dynamic_cast<QDragEnterEvent *>(event); if (dragEvent != nullptr) onDragEnterEvent(dragEvent); } else if (event->type() == QEvent::DragMove) { QDragMoveEvent *dragEvent = static_cast<QDragMoveEvent *>(event); auto *watchedWidget = dynamic_cast<QWidget *>(watched); if (watchedWidget != nullptr && dragEvent != nullptr) { const QPoint localMousePosition = watchedWidget->mapTo(this, dragEvent->position().toPoint()); onDragMoveEvent(localMousePosition); } } else if (event->type() == QEvent::DragLeave) { QDragLeaveEvent *dragEvent = static_cast<QDragLeaveEvent *>(event); if (dragEvent != nullptr) onDragLeaveEvent(); } else if (event->type() == QEvent::Drop) { QDropEvent *dropEvent = static_cast<QDropEvent *>(event); if (dropEvent != nullptr) onDropEvent(); } return QWidget::eventFilter(watched, event); }