2

在从我的应用程序中的 QListView 到像资源管理器这样的外部目标的(成功)拖放操作之后,我试图从 qt 获取任何类型的通知。

到目前为止我尝试过的事情:

  • QDropEvent:似乎只从内部小部件触发。
  • mouseReleaseEvent:当我使用 QDrop 时停止工作
  • 我尝试使用 pyhook 设置鼠标钩,以在拖动后捕捉鼠标。这适用于成功的丢弃,但在拒绝丢弃后挂起

任何指针都会有所帮助。

编辑

忘记密码

# -*- coding: utf-8 -*-

import sys
import pyHook 

from PySide import QtGui
from PySide import QtCore

app = QtGui.QApplication(sys.argv)


class MainWidget(QtGui.QWidget):
    def __init__(self):

        super(MainWidget, self).__init__()

        thumbViewModel = ThumbItemModel([ "item1" , "item2" , "item3" ])
        self.thumbView = ThumbnailView()
        self.thumbView.setModel(thumbViewModel)

        self.hm = pyHook.HookManager()
        self.hm.MouseLeftUp = self.onLeftMouseUp
        self.hm.HookMouse() #this will make the program unresponsive after an unsuccessful drop
        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(self.thumbView)

        self.setLayout(mainLayout)

        self.show()

    def onLeftMouseUp(self, event):
        print(event.Position)
        return True


class ThumbnailView(QtGui.QListView):
    def __init__(self, *args, **kwds):
        super(ThumbnailView, self).__init__(*args, **kwds)

        self.setDragEnabled(True)

    def mouseReleaseEvent(self, event):
        #only works with setDragEnabled(False)
        print('mouse released')

    def dropEvent(self, event):
        print('dropped')
        return QtGui.QListView.dropEvent(self, event)



    def startDrag(self, *args, **kwargs):
        print('drag started')
        return QtGui.QListView.startDrag(self, *args, **kwargs)



class ThumbItemModel(QtGui.QStringListModel):
    def __init__(self, *args, **kwds):
        super(ThumbItemModel, self).__init__(*args, **kwds)


    def supportedDropActions(self): 
        return QtCore.Qt.MoveAction | QtCore.Qt.CopyAction         

    def flags(self, index):
        if not index.isValid():
            return QtCore.Qt.ItemIsEnabled
        return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | \
               QtCore.Qt.ItemIsDragEnabled | QtCore.Qt.ItemIsDropEnabled        

    def mimeTypes(self):
        return ['text/uri-list', 'text/plain']

    def mimeData(self, indexes):
        mimedata = QtCore.QMimeData()
        fakeFile = 'file:///C:/matToObj.ms'
        mimedata.setData('text/uri-list', QtCore.QByteArray(fakeFile))
        return mimedata

    def dropMimeData(self, data, action, row, column, parent):
        print('dropMimeData %s %s %s %s' % (data.data('text/uri-list'), action, row, parent))
        return True


widget = MainWidget()
widget.show()

sys.exit(app.exec_())
4

1 回答 1

1

删除 pyHook 的东西,然后在ThumbnailView类中添加如下内容:

    def mousePressEvent(self, event):
        if (event.button() == QtCore.Qt.LeftButton):
            self.dragStartPosition = event.pos()

    def mouseMoveEvent(self, event):
        if (not (event.buttons() & QtCore.Qt.LeftButton)):
            return
        if ((event.pos() - self.dragStartPosition).manhattanLength() < QtGui.QApplication.startDragDistance()):
            return

        drag = QtGui.QDrag(self)
        mimeData = QtCore.QMimeData()

        mimeData.setData('text/uri-list', 'file:///C:/matToObj.ms')
        drag.setMimeData(mimeData)

        dropAction = drag.exec_(QtCore.Qt.CopyAction | QtCore.Qt.MoveAction)
        print 'dropped: %d'%dropAction

dropAction将包含一个指示丢弃是否失败或被接受的值,如下所示:http: //qt-project.org/doc/qt-4.8/qt.html#DropAction-enum(c++ 页面,很容易翻译到 Python)

请注意,此代码基于此处的示例:http: //qt-project.org/doc/qt-4.8/dnd.html#dragging

显然,您可能想要扩展它,以便 mime 数据实际上包含一个依赖于当时被拖动的特定项目的路径!您可能还想更改传递给的内容drag.exec_()

于 2013-10-19T12:08:02.370 回答