且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Qt:***的方式来委托拖放到孩子

更新时间:2022-12-31 08:32:23

拖放文档


为了能够接收媒体小部件,为小部件调用 setAcceptDrops(true),并重新实现 dragEnterEvent() dropEvent()事件处理函数。


以下是快速摘要:


  1. 孩子应该致电 setAcceptDrops(true)

  2. 子级应重新实现 dragEnterEvent 并接受其建议的操作

  3. 孩子应重新实现 dropEvent ,并接受其建议的操作

如果上述任何一项不是真的,则父母将有机会处理掉。


I'm using drag and drop on QWidget, I have reimplemented dragEnterEvent, dragLeaveEvent, dragMoveEvent and dropEvent, and that works well.

In my QWidget, I have other QWidget children, and I want them to manage drag and drop too. The issue is my parent QWidget catch the dnd events, so my children don't get it. What is the best way to say "when you are above a child widget, the child get the dnd events" ?

Thanks

Edit:

my widgets looks like this:

----------------------------------------
|            Parent Widget             |
|       ------------------             |
|       |  Child Widget  |             |
|       |                |             |
|       ------------------             |
|                                      |
----------------------------------------

my parent widget and child widget are of the same class.

The code is quite basic, it's similar to this:

DnDWidget::DnDWidget(QWidget* parent)
    : QWidget(parent)
{
    setAcceptDrops(true);
}

void DnDWidget::dragEnterEvent (QDragEnterEvent* event )
{
    qDebug() << "dragEnterEvent";
    if (event->mimeData()->hasFormat("foo/bar"))
        event->acceptProposedAction();
}

void DnDWidget::dragMoveEvent (QDragMoveEvent* event )
{
    qDebug() << "dragMoveEvent";
    //do visual feedback stuff
}

the dragEnterEvent of my child is never called.

From the drag-and-drop documentation:

To be able to receive media dropped on a widget, call setAcceptDrops(true) for the widget, and reimplement the dragEnterEvent() and dropEvent() event handler functions.

Here's the quick summary:

  1. The child should call setAcceptDrops(true).
  2. The child should reimplement the dragEnterEvent and accept its proposed action
  3. The child should reimplement the dropEvent and accept its proposed action.

If any of the above are not true then the parent will have a chance to handle the drop.