I'm attempting to get my code to work with multiple listViews instead of just one but am having an issue.
Working Code for one list:
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)
What I have so far to use multiple lists:
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)
So I get an error with "self.itemDropped(self.listView)" and after research on here and other sites I came up with this:
self.connect(self.listView, QtCore.SIGNAL("dropped"),(lambda : self.itemDropped(self.listView)))
I had never used the lambda function before because I am quite new to python but that did fix the issue, when i print listName it shows up correctly. The problem now is that the links aren't emitting correctly from the other class, or rather I'm not receiving them correctly.
So I guess pseduocode i need something like this:
self.connect(self.listView, QtCore.SIGNAL("dropped"),(lambda : self.itemDropped(X, self.listView)))
The question is how do I get X, that is the links from the TestListView class? I don't quite understand how I was receiving them with just 1 list when there were no variables being passed to the function.
Thanks for any help you guys can provide i greatly appreciate it. P.s. Code from here might look familiar if you want a bigger picture PyQT4: Drag and drop files into QListWidget
What you want is this
self.connect(self.listView, QtCore.SIGNAL("dropped"),(lambda X: self.itemDropped(X, self.listView)))
When you emit your signal, you are passing the links
variable into the slot, which used to be self.itemDropped
(The signature of which was self.itemDropped (links)
).
Instead you slot is now a lambda function, and so you need to define it as a function of 1 variable by starting the definition with lambda X:
. This then makes X
available to the rest of the lambda definition.
When your signal is emitted, your lambda function is called and X
contains links
.
In general: def foo(x): do_something(x) foo(3)
is equivalent to my_function = lambda x: do_something(x) my_function(3)
Make sense?
EDIT: I should also point out (for any future applications you have) that there are some tricky points when using variables inside lambda functions (specifically the use of variables that are not specified in the definition of the lambda function, such as the use of self.listView
). When the lambda function is called (when the signal is emitted), it will use whatever the current value of self.listView
is, not the value it was when the function was defined. This becomes a problem when defining lambda functions inside a loop and trying to use a loop variable inside your lambda function. Some useful information can be found here (read the comments too) http://eli.thegreenplace.net/2011/04/25/passing-extra-arguments-to-pyqt-slot/