我无法QListWidget
接受丢弃的文件。
我曾经Qt Designer
制作过QMainWindow
和QListWidget
。
我怎么解决这个问题?
这是我的main.py
import sys
import os
from PyQt4 import QtCore, QtGui
from PythonApp import Ui_MainWindow
class MainWindow(Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
listView = Ui_MainWindow.listView
listView.setAcceptDrops(True)
listView.setIconSize(QtCore.QSize(72, 72))
self.connect(listView, QtCore.SIGNAL("dropped"), self.pictureDropped)
self.setCentralWidget(listView)
def pictureDropped(self, l):
listView = Ui_MainWindow.listView
for url in l:
if os.path.exists(url):
print(url)
icon = QtGui.QIcon(url)
pixmap = icon.pixmap(72, 72)
icon = QtGui.QIcon(pixmap)
item = QtGui.QListWidgetItem(url, listView)
item.setIcon(icon)
item.setStatusTip(url)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls:
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(QtCore.Qt.CopyAction)
event.accept()
links = []
for url in event.mimeData().urls():
links.append(str(url.toLocalFile()))
self.emit(QtCore.SIGNAL("dropped"), links)
else:
event.ignore()
def main():
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
这是我的PythonApp.py
文件:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(800, 600)
self.centralwidget = QtGui.QWidget(MainWindow)
self.centralwidget.setAcceptDrops(True)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.listView = QtGui.QListView(self.centralwidget)
self.listView.setGeometry(QtCore.QRect(100, 60, 501, 331))
self.listView.setAcceptDrops(True)
self.listView.setDragDropMode(QtGui.QAbstractItemView.DragDrop)
self.listView.setDefaultDropAction(QtCore.Qt.CopyAction)
self.listView.setObjectName(_fromUtf8("listView"))
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtGui.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 22))
self.menubar.setObjectName(_fromUtf8("menubar"))
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtGui.QStatusBar(MainWindow)
self.statusbar.setObjectName(_fromUtf8("statusbar"))
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8))