PyQT4:将文件拖放到QListWidget中

时间:2010-11-11 05:36:13

标签: python drag-and-drop pyqt

我一直在编写OCR书籍扫描内容(它通过读取页码重命名页面),并从我的基本CLI Python脚本切换到GUI。

我正在使用PyQT4并查看了大量关于拖放的文档,但没有运气。它只是拒绝接受这些文件!我正在将这些文章用于我的UI设计:

  1. http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/

  2. http://zetcode.com/tutorials/pyqt4/dragdrop/

  3. 我注意到有很多方法可以设置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))
    

    感谢任何可以提供帮助的人!

1 个答案:

答案 0 :(得分:16)

您使用的代码作为示例似乎工作正常,看起来很干净。根据您的评论,您的列表小部件未初始化;这应该是您的问题的根本原因。我已经在我的Ubuntu 10.04LTS上试了一下你的代码,它工作得很好。我的代码列在下面,看看它是否适合你。您应该能够将文件拖放到列表小部件中;一旦它被删除,就会添加一个新项目,显示图像和图像的文件名。

import sys
import os
from PyQt4 import QtGui, QtCore

class TestListView(QtGui.QListWidget):
    def __init__(self, type, parent=None):
        super(TestListView, self).__init__(parent)
        self.setAcceptDrops(True)
        self.setIconSize(QtCore.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(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()

class MainForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        self.view = TestListView(self)
        self.connect(self.view, QtCore.SIGNAL("dropped"), self.pictureDropped)
        self.setCentralWidget(self.view)

    def pictureDropped(self, l):
        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, self.view)
                item.setIcon(icon)        
                item.setStatusTip(url)        

def main():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

希望这有帮助,尊重