Python PyQt-添加拖放;拖放到小部件

Python PyQt-添加拖放;拖放到小部件,python,pyqt,pyqt5,Python,Pyqt,Pyqt5,我有一个从QWidget继承的类: from PyQt5.QtWidgets import * class Widget(QWidget): def __init__(self, layoutname: str, width: int, height: int ) -> None: """ Constructor, pass 0 to width and/or height if you want them to be defaults give

我有一个从QWidget继承的类:

from PyQt5.QtWidgets import *


class Widget(QWidget):
    def __init__(self, layoutname: str, width: int, height: int ) -> None:
        """
        Constructor, pass 0 to width and/or height if you want them to be defaults given at runtime
        :param layoutname: Layout, a Layout object will be built according to the class name passed
        :param width: int,  width of the widget
        :param height: int, height of the widget
        """
        super(QWidget, self).__init__()

    if width != 0:
        self.setFixedWidth(width)

    if height != 0:
        self.setFixedHeight(height)

    layout = layoutname()
    self.setLayout(layout)


def addChild(self, child: object)-> None:
    """
    Adds child as a child to the widget
    :param child: Widget, the child you want to add to the current widget
    """
    self.layout().addWidget(child)

def droppedR(self):
    print("DROPPED")

我想添加以下特性:当在小部件上删除某些内容(比如文件)时,我想调用droppedR()。我该怎么做呢?

我不确定它是否准确地回答了您的要求,但我以前在dragDrop方面遇到过一些问题,也许我花了很长时间的这个问题和答案可以帮助您

总而言之,主要理由是:

  • 使用
    Accept()
    acceptProposeAction()
    方法接受dragDrop事件
  • 重新执行
    dragEnterEvent
    dragmovevent
    dropEvent
  • 在这些方法中,你应该做你想做的事情。例如,在
    dropEvent
    方法中调用droppedR方法

  • 这取决于你想在每个事件中应用的逻辑。

    我不确定它是否准确地回答了你想要的,但我以前在dragDrop方面遇到过一些问题,也许我花了很长时间的这个问题和答案可以帮到你

    总而言之,主要理由是:

  • 使用
    Accept()
    acceptProposeAction()
    方法接受dragDrop事件
  • 重新执行
    dragEnterEvent
    dragmovevent
    dropEvent
  • 在这些方法中,你应该做你想做的事情。例如,在
    dropEvent
    方法中调用droppedR方法
  • 在每个事件中应用的逻辑取决于您