Python PyQt使用多个列表视图连接到一个带有信号/插槽的函数
我试图让我的代码使用多个列表视图,而不是一个,但遇到了一个问题 一个列表的工作代码:Python PyQt使用多个列表视图连接到一个带有信号/插槽的函数,python,listview,pyqt,signals-slots,Python,Listview,Pyqt,Signals Slots,我试图让我的代码使用多个列表视图,而不是一个,但遇到了一个问题 一个列表的工作代码: Class Ui_MainWindow(QtGui.QMainWindow): def itemDropped(self, links): item = QtGui.QListWidgetItem(url, self.listView) def setupUi(self, MainWindow): self.connect(self.listView, QtCor
Class Ui_MainWindow(QtGui.QMainWindow):
def itemDropped(self, links):
item = QtGui.QListWidgetItem(url, self.listView)
def setupUi(self, MainWindow):
self.connect(self.listView, QtCore.SIGNAL("dropped"), self.itemDropped)
Class TestListView(QtGui.QListWidget):
def dropEvent(self, event):
self.emit(QtCore.SIGNAL("dropped"), links)
到目前为止,我可以使用多个列表:
Class Ui_MainWindow(QtGui.QMainWindow):
def itemDropped(self, links, listName):
item = QtGui.QListWidgetItem(url, listName)
def setupUi(self, MainWindow):
self.connect(self.listView, QtCore.SIGNAL("dropped"), self.itemDropped(self.listView))
Class TestListView(QtGui.QListWidget):
def dropEvent(self, event):
self.emit(QtCore.SIGNAL("dropped"), links)
因此,我在“self.itemdroped(self.listView)”中遇到了一个错误,在对此处和其他网站进行研究后,我得出了以下结论:
self.connect(self.listView, QtCore.SIGNAL("dropped"),(lambda : self.itemDropped(self.listView)))
我以前从未使用过lambda函数,因为我对python非常陌生,但这确实解决了问题,当我打印listName时,它会正确显示。现在的问题是链接没有正确地从另一个类发出,或者说我没有正确地接收它们
所以我想psedoocode我需要这样的东西:
self.connect(self.listView, QtCore.SIGNAL("dropped"),(lambda : self.itemDropped(X, self.listView)))
问题是如何获得X,即来自TestListView类的链接?我不太明白,当没有变量被传递到函数时,我是如何仅用一个列表来接收它们的
谢谢你们的帮助,我非常感激。
如果你想了解更大的情况,这里的代码可能看起来很熟悉
self.connect(self.listView, QtCore.SIGNAL("dropped"),(lambda X: self.itemDropped(X, self.listView)))
当您发出信号时,您正在将links
变量传递到插槽中,该插槽以前是self.itemdroped
(其签名是self.itemdroped(links)
)
相反,插槽现在是一个lambda函数,因此需要通过使用lambda X:
开始定义,将其定义为1个变量的函数。这将使X
可用于lambda定义的其余部分
当发出信号时,将调用lambda函数,X
包含链接
一般而言:
def foo(x):
做某事(x)
傅(3)
相当于
my_function=lambda x:do_something(x)
my_功能(3)
有道理吗
编辑:我还应该指出(对于将来的任何应用程序),在lambda函数中使用变量时存在一些棘手的问题(特别是使用lambda函数定义中未指定的变量,例如使用
self.listView
)。调用lambda函数时(当发出信号时),它将使用self.listView
的当前值,而不是定义函数时的值。在循环中定义lambda函数并尝试在lambda函数中使用循环变量时,这会成为一个问题。这里可以找到一些有用的信息(也可以阅读评论)哇,这非常有意义,工作完美,我喜欢我的“伪代码”只差一个字母,还有你发布的链接,我不知道谷歌怎么找不到,这正是我需要的,我必须在那里挖掘,看看我还能学到什么,谢谢!