最终了解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()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)