找回密码
 立即注册
收起左侧

《快速入门》5-8例子中的event->accept();问题

3
回复
7325
查看
[复制链接]
累计签到:2 天
连续签到:1 天
来源: 2014-12-18 16:09:43 显示全部楼层 |阅读模式
10Qter豆
第五章 例子 5-8
  1. // 拖动进入事件
  2. void MainWindow::dragEnterEvent(QDragEnterEvent *event)
  3. {
  4.     qDebug()<<__func__;
  5.      // 如果有我们定义的MIME类型数据,则进行移动操作
  6.      if (event->mimeData()->hasFormat("myimage/png")) {
  7.              event->setDropAction(Qt::MoveAction);
  8.              event->accept(); // ---1
  9.      } else {
  10.          event->ignore();
  11.      }
  12. }

  13. // 拖动事件
  14. void MainWindow::dragMoveEvent(QDragMoveEvent *event)
  15. {
  16.     qDebug()<<__func__;
  17.      if (event->mimeData()->hasFormat("myimage/png")) {
  18.              event->setDropAction(Qt::MoveAction);
  19.              event->accept(); // --- 2
  20.      } else {
  21.          event->ignore();
  22.      }
  23. }
复制代码
去掉第一个event->accept(); 就不能拖动了。
去掉第二个和放下事件void MainWindow::dropEvent(QDropEvent *event)
中的
event->accept();程序运行效果不变,为什么?
event->accept(); 表示标记这个事件已经处理过了,不用交予父对象处理。不过就算没这句话,将事件传递了,应该也没关系吧?

求教。



最佳答案

查看完整内容

嗯。还是看文档:A widget must accept this event in order to receive the drag move events that are sent while the drag and drop action is in progress. 在dragEnterEvent中必须接受,不然就不能接收到这个事件。后面的accept()你测试可以去掉,不过建议还是留着。
回复

使用道具 举报

累计签到:1569 天
连续签到:1 天
2014-12-18 16:09:44 显示全部楼层
嗯。还是看文档:A widget must accept this event in order to receive the drag move events that are sent while the drag and drop action is in progress.

在dragEnterEvent中必须接受,不然就不能接收到这个事件。后面的accept()你测试可以去掉,不过建议还是留着。
回复

使用道具 举报

累计签到:597 天
连续签到:1 天
2014-12-18 19:11:43 显示全部楼层
void QWidget::dragEnterEvent (QDragEnterEvent * event);
        This event handler is called when a drag is in progress and the mouse enters this widget. The event is passed in the event parameter.
If the event is ignored, the widget won't receive any drag move events.

void QWidget::dragMoveEvent ( QDragMoveEvent * event );
        This event handler is called if a drag is in progress, and when any of the following conditions occur: the cursor enters this widget, the cursor moves within this widget, or a modifier key is pressed on the keyboard while this widget has the focus. The event is passed in the event parameter.

void QDragMoveEvent::accept ( const QRect & rectangle );
        The same as accept(), but also notifies that future moves will also be acceptable if they remain within the rectangle given on the widget. This can improve performance, but may also be ignored by the underlying system.
        If the rectangle is empty, drag move events will be sent continuously. This is useful if the source is scrolling in a timer event.

摘了英文手册,希望有所帮助
回复

使用道具 举报

累计签到:2 天
连续签到:1 天
2014-12-19 09:36:08 显示全部楼层
明白了谢谢两位。我看的不够仔细。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了