我想在dropEvent of a QVideoWidget上播放一个媒体。我知道扮演的角色,我的问题是QVideoWidget没有像其他小部件那样接收dropEvent。我很奇怪这是个虫子或者我漏掉了什么东西。
一切正常,所有三个事件都被调用:
import sys
from PySide6.QtCore import QSize
from PySide6.QtMultimediaWidgets import QVideoWidget
from PySide6.QtWidgets import QApplication, QWidget
class MyWidget(QWidget):
def __init__(self,*args):
super().__init__(*args)
self.setAcceptDrops(True)
self.setMinimumSize(QSize(600,400))
def dragEnterEvent(self, event):
print(event)
event.accept()
def dragMoveEvent(self, event):
print(event)
event.accept()
def dropEvent(self, event):
print(event)
if __name__ == "__main__":
app = QApplication([])
widget = MyWidget()
widget.show()
sys.exit(app.exec())但是,当我将相同的代码与一个QVideoWidget dropEvent 一起使用时, dropEvent就不会被调用:
import sys
from PySide6.QtCore import QSize
from PySide6.QtMultimediaWidgets import QVideoWidget
from PySide6.QtWidgets import QApplication, QWidget
class MyWidget(QVideoWidget):
def __init__(self,*args):
super().__init__(*args)
self.setAcceptDrops(True)
self.setMinimumSize(QSize(600,400))
def dragEnterEvent(self, event):
print(event)
event.accept()
def dragMoveEvent(self, event):
print(event)
event.accept()
def dropEvent(self, event):
print(event)
if __name__ == "__main__":
app = QApplication([])
widget = MyWidget()
widget.show()
sys.exit(app.exec())我不知道为什么,但在最后一段代码中,dropEvent在标题栏上工作!如果需要的话我可以上传一个GIF。
发布于 2021-11-18 19:11:16
您已经发现了一个bug,QVideoWidget在Qt6中使用QWindow来加速绘制,这是作为QVideoWidget的一个子元素添加的,因此拖放事件不会传播到父进程。解决办法是使用事件筛选器来侦听事件。
import sys
from PySide6.QtCore import QSize, QEvent
from PySide6.QtWidgets import QApplication, QWidget
from PySide6.QtMultimediaWidgets import QVideoWidget
class MyWidget(QVideoWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setMinimumSize(QSize(600, 400))
self.window_child.installEventFilter(self)
@property
def window_child(self):
child = self.findChild(QWidget)
if child.metaObject().className() == "QWindowContainer":
return child
def eventFilter(self, obj, event):
if obj is self.window_child:
if event.type() == QEvent.Type.DragEnter:
print("dragEnterEvent:", event)
elif event.type() == QEvent.Type.DragMove:
print("dragMoveEvent:", event)
elif event.type() == QEvent.Type.Drop:
print("dropEvent", event)
return super().eventFilter(obj, event)
if __name__ == "__main__":
app = QApplication([])
widget = MyWidget()
widget.show()
sys.exit(app.exec())发布于 2022-10-14 18:09:56
谢谢你救了我的命!我也遇到了同样的问题,并编写了一个Qt错误报告(到目前为止还没有找到:https://bugreports.qt.io/browse/QTBUG-107668)。在C++中,如下所示:
VideoWidget::VideoWidget(QWidget *parent) : QVideoWidget(parent)
{
QObject *c = findChild<QWidget*>();
c->installEventFilter(this);
}
bool VideoWidget::eventFilter(QObject *obj, QEvent *ev)
{
QObject *c = findChild<QWidget*>();
if(obj == c){
if(ev->type() == QEvent::DragEnter){
qDebug()<<"Drag Enter";
return true;
}
else if(ev->type() == QEvent::DragMove){
//qDebug()<<"Drag Move";
return true;
}
else if(ev->type() == QEvent::Drop){
qDebug()<<"Drop";
return true;
}
}
return QVideoWidget::eventFilter(obj, ev);
}https://stackoverflow.com/questions/70024698
复制相似问题