我正试图从我的应用程序中的QListView(成功)拖放操作到外部目标(如资源管理器)之后从qt获得任何类型的通知。
到目前为止我尝试过的事情:
任何指针都会有所帮助。
修改
忘记了代码
# -*- 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_())
答案 0 :(得分: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_()