【问题标题】:PyQt - Add drag & drop to widgetPyQt - 添加拖放到小部件
【发布时间】:2017-06-02 13:31:35
【问题描述】:

我有一个继承自 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")

我想添加以下功能:当在小部件上放置某些东西(比如文件)时,我想调用 dropR()。我该怎么做?

【问题讨论】:

    标签: python pyqt pyqt5


    【解决方案1】:

    我不确定它是否能准确地回答你想要的,但我之前在使用 dragDrop 时遇到了一些问题,也许这个我花了很长时间的问答可以帮助你here

    总结一下,主要的推理是:

    1. 使用accept()acceptProposedAction() 方法接受dragDrop 事件
    2. 重新实现dragEnterEventdragMoveEventdropEvent
    3. 在这些方法中,您应该做您想做的事。例如,在 dropEvent 方法中调用您的 dropR 方法。

    由您决定要在每个事件中应用的逻辑。

    【讨论】:

      猜你喜欢
      • 2019-05-07
      • 2012-08-14
      • 2014-08-16
      • 1970-01-01
      • 2020-04-08
      • 2012-02-05
      • 2017-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多