2017-06-02 38 views
1

我有一个类,从QWidget的继承:PyQt的 - 添加拖放窗口小部件

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()。我怎样才能做到这一点 ?

回答

0

我不确定它是否正确地回答了您的要求,但是我之前使用dragDrop出现过一些问题,也许这个问题和答案我花了很长时间可以帮助您解决问题here

汇总,主要理由是:

  1. 接受的DragDrop事件与accept()acceptProposedAction()方法
  2. 重新实现dragEnterEventdragMoveEventdropEvent
  3. 在这些方法里面你应该做你想做的。例如,在dropEvent方法中调用你的droppedR方法。

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