简体   繁体   English

pyqt4在Qlistview中拖放文件

[英]pyqt4 drag and drop files in Qlistview

i can not find drag and drop the files in qlistview. 我在qlistview中找不到拖放文件。

i find sample code. 我找到示例代码。

but this is qlistwidget and qpushbutton 但这是qlistwidget和qpushbutton

also this sample is using class 这个样本也在使用类

i want to make drag and drop below example. 我想在下面的示例中进行拖放。

Why can not use the dropped in qlistview? 为什么不能使用在qlistview中删除的内容?

main.py 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 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
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)

And in my example. 在我的例子中。

self.verticalLayout.addWidget(listView)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM