PySide:将文件拖放到QListWidget中

PySide:将文件拖放到QListWidget中,第1张

PySide:将文件拖放到QListWidget中

最终了解PySide信号的工作方式。这是上面的PyQt4代码,已移植到PySide。我很想听听是否有更好的解决方案。

import sysimport osfrom PySide import QtGui, QtCoreclass TestListView(QtGui.QListWidget):    fileDropped = QtCore.Signal(list)    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.fileDropped.emit(links)        else: event.ignore()class MainForm(QtGui.QMainWindow):    def __init__(self, parent=None):        super(MainForm, self).__init__(parent)        self.view = TestListView(self)        self.view.fileDropped.connect(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()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5562392.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存