Qt:最好的方式来委托拖放到孩子 [英] Qt: best way to delegate a drag and drop to children

查看:170
本文介绍了Qt:最好的方式来委托拖放到孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在QWidget上使用拖放,我重新实现了dragEnterEvent,dragLeaveEvent,dragMoveEvent和dropEvent,并且效果很好。



在我的QWidget其他QWidget的孩子,我想让他们管理拖放太。问题是我的父QWidget捕获dnd事件,所以我的孩子不会得到它。



感谢



编辑:



我的小部件如下所示:

  ---------------------------------------- 
|父控件|
| ------------------ |
| |子部件| |
| | | |
| ------------------ |
| |
----------------------------------------



我的父窗口小部件和子窗口小部件属于同一类。



代码是相当基本的,它类似于:

  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;
//做视觉反馈的东西
}

我的孩子的dragEnterEvent从未调用。

解决方案

拖放文档


为了能够接收媒体小部件,为小部件调用 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.

这篇关于Qt:最好的方式来委托拖放到孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆