我一直在編寫一個OCR書籍掃描事物(它通過讀取頁面編號重命名頁面),並已從我的基本CLI Python腳本切換到GUI。PyQT4:將文件拖放到QListWidget中
我正在使用PyQT4並查看了大量關於拖放的文檔,但沒有運氣。它只是拒絕接受這些文件!我用這些來爲我的UI設計的文章:
我注意到,有很多方法來建立一個PyQt4的GUI一噸。哪一個最好?
糟糕,這裏是項目的源代碼。
主要腳本:
import sys
from PyQt4 import QtCore
from PyQt4 import QtGui
from PyQt4.QtGui import QListWidget
from layout import Ui_window
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_window()
self.ui.setupUi(self)
QtCore.QObject.connect(self.ui.listWidget, QtCore.SIGNAL("dropped"), self.picture_dropped)
def picture_dropped(self, l):
for url in l:
if os.path.exists(url):
picture = Image.open(url)
picture.thumbnail((72, 72), Image.ANTIALIAS)
icon = QIcon(QPixmap.fromImage(ImageQt.ImageQt(picture)))
item = QListWidgetItem(os.path.basename(url)[:20] + "...", self.pictureListWidget)
item.setStatusTip(url)
item.setIcon(icon)
class DragDropListWidget(QListWidget):
def __init__(self, type, parent = None):
super(DragDropListWidget, self).__init__(parent)
self.setAcceptDrops(True)
self.setIconSize(QSize(72, 72))
def dragEnterEvent(self, event):
if event.mimeData().hasUrls:
event.accept()
else:
event.ignore()
def dragMoveEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(Qt.CopyAction)
event.accept()
else:
event.ignore()
def dropEvent(self, event):
if event.mimeData().hasUrls:
event.setDropAction(Qt.CopyAction)
event.accept()
l = []
for url in event.mimeData().urls():
l.append(str(url.toLocalFile()))
self.emit(SIGNAL("dropped"), l)
else:
event.ignore()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_())
和UI文件...
# Form implementation generated from reading ui file 'layout.ui'
#
# Created: Thu Nov 11 00:22:52 2010
# by: PyQt4 UI code generator 4.8.1
#
# WARNING! All changes made in this file will be lost!
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
_fromUtf8 = lambda s: s
class Ui_window(object):
def setupUi(self, window):
window.setObjectName(_fromUtf8("window"))
window.resize(543, 402)
window.setAcceptDrops(True)
self.centralwidget = QtGui.QWidget(window)
self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
self.listWidget = QtGui.QListWidget(self.centralwidget)
self.listWidget.setProperty(_fromUtf8("cursor"), QtCore.Qt.SizeHorCursor)
self.listWidget.setAcceptDrops(True)
self.listWidget.setObjectName(_fromUtf8("listWidget"))
self.verticalLayout.addWidget(self.listWidget)
window.setCentralWidget(self.centralwidget)
self.retranslateUi(window)
QtCore.QMetaObject.connectSlotsByName(window)
def retranslateUi(self, window):
window.setWindowTitle(QtGui.QApplication.translate("window", "PyNamer OCR", None, QtGui.QApplication.UnicodeUTF8))
感謝任何人誰可以幫助!
只是可以肯定,沒有「如果event.mimeData()。hasUrls」在dropEvent評估爲真,當你拖動文件到它? – 2010-11-11 06:07:22
我在每個函數中都添加了print()語句,而且它甚至沒有初始化控件! * sobs * – Blender 2010-11-12 13:46:05