2017-06-02 92 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方法。

這取決於你想要在每個事件中應用的邏輯。