我有一組3個列表小部件,都具有拖放功能。我正在尋找消除重複的代碼,並尋求一些幫助。正如你可以在我的Dialog類中看到的那樣,我創建了3個拖放和drog列表的圖像類。截至目前,每個List Widget都對應一個連接功能。PyQt:參數化連接功能
self.connect(self.one, SIGNAL("dropped"), self.oneDropped)
在我看來,該功能def oneDropped()
可以變成一個函數,其中self.oneDropped
一種說法,但我有麻煩設置,並可以使用一些幫助。這是我的代碼。
我只用了幾周的Python工作,所以如果這是一個簡單的問題,我很抱歉。
import sys
import os
from PyQt4 import uic
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Image(QListWidget):
def __init__(self, type, parent=None):
super(Image, 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()
links = []
for url in event.mimeData().urls():
links.append(str(url.toLocalFile()))
self.emit(SIGNAL("dropped"), links)
else:
event.ignore()
class Dialog(QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.ui = uic.loadUi('./qt/default.ui', self)
self.one = Image(self)
self.two = Image(self)
self.three = Image(self)
self.connect(self.one, SIGNAL("dropped"), self.oneDropped)
self.connect(self.two, SIGNAL("dropped"), self.twoDropped)
self.connect(self.three, SIGNAL("dropped"), self.threeDropped)
self.baseimage_layout.addWidget(self.one)
self.redimage_layout.addWidget(self.two)
self.greenimage_layout.addWidget(self.three)
def oneDropped(self, l):
for url in l:
if os.path.exists(url):
print(url)
icon = QIcon(url)
pixmap = icon.pixmap(72, 72)
icon = QIcon(pixmap)
item = QListWidgetItem(url, self.one)
item.setIcon(icon)
def twoDropped(self, l):
for url in l:
if os.path.exists(url):
print(url)
icon = QIcon(url)
pixmap = icon.pixmap(72, 72)
icon = QIcon(pixmap)
item = QListWidgetItem(url, self.two)
item.setIcon(icon)
def threeDropped(self, l):
for url in l:
if os.path.exists(url):
print(url)
icon = QIcon(url)
pixmap = icon.pixmap(72, 72)
icon = QIcon(pixmap)
item = QListWidgetItem(url, self.three)
item.setIcon(icon)
def main():
app = QApplication(sys.argv)
form = Dialog()
form.show()
app.exec_()
if __name__=="__main__":
main()
我接受了其他答案,因爲它似乎與我目前的設置很好。話雖如此,你的答案似乎更現代化。我仍在閱讀如何實施新風格。 您對lambda的反應是對新型機制的單獨解決方案,還是打算使用過時的代碼? 再次道歉,如果這是一個簡單的問題。 –
它也應該使用舊的機制。只需用lambda表達式替換'self.oneDropped'等參考。 – user3419537
啊我現在看到了,謝謝! –