简体   繁体   English

Qt 没有收到 QWidget::dropEvent

[英]Qt not receiving QWidget::dropEvent

I drag a QStandardItem from a QListView over a QWidget.我将 QStandardItem 从 QListView 拖到 QWidget 上。 In my QWidget class I implemented dragEnterEvent and dropEvent:在我的 QWidget 类中,我实现了 dragEnterEvent 和 dropEvent:

void MyWidget::dragEnterEvent(QDragEnterEvent *event){
    qDebug() << "drag entering: ";
}

void MyWidget::dropEvent(QDropEvent *event){
    qDebug() << "drop received!";
}

It receives the dragEntering, but not the dropping.它接收dragEntering,但不接收drop。 I have already set the acceptDrops to true in the constructor of the QWidget class.我已经在 QWidget 类的构造函数中将acceptDrops设置为 true 。 What am I missing?我错过了什么? Thanks for help!感谢帮助!

Well, I finally got the solution.嗯,我终于得到了解决方案。 I had to add event->acceptProposedAction();我不得不添加event->acceptProposedAction(); in the dragEnterEvent in the widget class.在小部件类中的 dragEnterEvent 中。

Another cause of failure is an incorrect event type (copy and paste).另一个失败的原因是不正确的事件类型(复制和粘贴)。 Ensure they match the event handler:确保它们匹配事件处理程序:

dragEnterEvent(QDragEnterEvent *event)
dragMoveEvent(QDragMoveEvent *event)
dropEvent(QDropEvent *event)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM