我无法在qlistview中找到拖放文件。
我找到了示例代码。
但这是qlistwidget和qpushbutton
此示例也使用了类
我想在示例下拖放。
为什么不能在qlistview中使用drop?
main.py
from PyQt4 import QtCore, QtGui
from test import *
import sys
class main(QtGui.QMainWindow):
def __init__(self,parent=None):
global app
QtGui.QWidget.__init__(self,parent)
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
app=QtGui.QApplication(sys.argv)
myapp=main()
myapp.show()
app.exec_()
test.py
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(273, 214)
self.listView = QtGui.QListView(Form)
self.listView.setGeometry(QtCore.QRect(10, 10, 256, 192))
self.listView.setObjectName(_fromUtf8("listView"))
self.listView.setAcceptDrops(True)
self.listView.dropped.connect(mydragdrop)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
def mydragdrop(self):
None
答案 0 :(得分:1)
\param
在我的例子中。
class DragAndDrop(QListView):
def __init__(self, parent):
global listView
super(DragAndDrop, self).__init__(parent)
self.setAcceptDrops(True)
self.setDragDropMode(QAbstractItemView.InternalMove)
self.setFrameShadow(QFrame.Plain)
self.setFrameShape(QFrame.Box)
listView=self
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.acceptProposedAction()
else:
super(DragAndDrop, self).dragEnterEvent(event)
def dragMoveEvent(self, event):
super(DragAndDrop, self).dragMoveEvent(event)
def dropEvent(self, event):
#global itemModel
if event.mimeData().hasUrls():
for url in event.mimeData().urls():
if url.path()[-5:]==".pcap":
if itemModel.findItems(url.path()[1:].replace("/","\\")):
pass
else:
item=QStandardItem(url.path()[1:].replace("/","\\"))
item.setCheckable(True)
itemModel.appendRow(item)
else:
pass
event.acceptProposedAction()
self.setModel(itemModel)
else:
super(DragAndDrop,self).dropEvent(event)