dropEvent()没有被调用

时间:2010-11-11 22:29:48

标签: python pyqt

我正在尝试从QTreeWidget执行拖放操作到QGraphicsView。 dragStart()工作,dragEnterEvent()工作,但从不调用dropEvent()。在光标进入QGraphicsView之前,pixmap也没有出现,这不是问题,但我只是觉得它会在拖动开始时出现。这是我的startDrag函数:

def on_list_startDrag(self, supportedActions):
    #Retreive the item that was clicked on
    currentPart = self.ui.list.currentItem()
    part = currentPart.text(0)

    drag = QtGui.QDrag(self.ui.list)
    mime = QtCore.QMimeData()
    print(part)
    #retreive that associated graphics file
    icon = QtGui.QIcon('drawings/FULL/' + part + '.svg')
    pixmap = icon.pixmap(102,122)

    selected = QtGui.QImage('drawings/FULL/' + part + '.svg')
    data = pickle.dumps(selected)

    mime.setData('application/x-item', data)
    #mime.setImageData(QtGui.QImage('drawings/FULL/' + part + '.svg'))
    drag.setMimeData(mime)
    drag.setHotSpot(QtCore.QPoint(pixmap.width()/2, pixmap.height()/2))
    drag.setPixmap(pixmap)
    drag.exec_()

这是dragEnterEvent:

def on_workArea_dragEnterEvent(self, event):
    print(event.format())
    if (event.mimeData().hasFormat('application/x-item')):
        event.accept()
        print('accepted')
    else:
        event.ignore()

最后是dropEvent代码:

def on_workArea_dropEvent(self, event):
    print('dropped')

当我开始执行拖放操作时,光标有一个带斜杠的圆圈,就像小部件不接受掉落一样,但是我设置了QGraphicsView,workArea来接受drop。有人可以帮助我让drop工作并解释为什么pixmap不会出现在光标后面,直到光标在QGraphicsView上。谢谢。

2 个答案:

答案 0 :(得分:3)

您也需要实施dragMoveEvent(),否则将不会调用dropEvent()。这也是导致它显示正确的拖放图标的原因,而不是斜线圈“不能放在这里”的图标。

答案 1 :(得分:0)

我已经检查了你的代码,它确实对我来说很好看; dropEvent和pixmap都按预期工作。也许你的代码中有其他东西会导致你所描述的这种不受欢迎的行为。对于dropEvent,您可能在将插槽连接到信号时遇到问题,这会导致您的代码无法被调用。我做了一个小例子,它在树视图和grahicsview之间拖放并加载并呈现像素图:

import sys
from PyQt4 import QtGui, QtCore

class TestTreeView(QtGui.QTreeView):
    def __init__(self, parent = None):
        super(TestTreeView, self).__init__(parent)
        self.setDragEnabled(True)

    def startDrag(self, dropAction):
        print('tree start drag')

        icon = QtGui.QIcon('/home/image.png')
        pixmap = icon.pixmap(64, 64)

        mime = QtCore.QMimeData()
        mime.setData('application/x-item', '???')

        drag = QtGui.QDrag(self)
        drag.setMimeData(mime)        
        drag.setHotSpot(QtCore.QPoint(pixmap.width()/2, pixmap.height()/2))
        drag.setPixmap(pixmap)        
        drag.start(QtCore.Qt.CopyAction)

class TestGraphicsView(QtGui.QGraphicsView): 
    def __init__(self, parent = None):
        super(TestGraphicsView, self).__init__(parent)
        self.setAcceptDrops(True)

    def dragEnterEvent(self, event):
        print('graphics view drag enter')
        if (event.mimeData().hasFormat('application/x-item')):
            event.acceptProposedAction()
            print('accepted')
        else:
            event.ignore()    

    def dropEvent(self, event): 
        print('graphics view drop')
        event.acceptProposedAction()                 

class MainForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        self.model = QtGui.QStandardItemModel()

        for k in range(0, 4):
            parentItem = self.model.invisibleRootItem()
            for i in range(0, 4):
                item = QtGui.QStandardItem(QtCore.QString("item %0 %1").arg(k).arg(i))
                parentItem.appendRow(item)
                parentItem = item

        self.setMinimumSize(300, 400)

        self.view = TestTreeView(self)
        self.view.setModel(self.model)
        self.view.setMinimumSize(300, 200)

        self.graphicsView = TestGraphicsView(self)
        self.graphicsView.setGeometry(0, 210, 300, 400)        

        self.layout = QtGui.QVBoxLayout(self.centralWidget())        
        self.layout.addWidget(self.view)
        self.layout.addWidget(self.graphicsView)

def main():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main() 

希望这有帮助,尊重