Hello everyone. I am making simple model/view application using python3.4 and PyQt5 in Windows 7.
First of all, here is my code.
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QListView
from PyQt5.Qt import QStandardItemModel, QStandardItem
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.list = QListView(self)
model = QStandardItemModel(self.list)
carMaker = ['Ford', 'GM', 'Renault', 'VW', 'Toyota']
for maker in carMaker:
item = QStandardItem(maker)
item.setCheckable(True)
model.appendRow(item)
self.list.setModel(model)
model.itemChanged.connect(self.on_item_changed)
#model.itemChanged.connect(functools.partial(self.on_item_changed, item, 1))
#model.itemChanged.connect(lambda: self.on_item_changed(item, 2))
self.list.setMinimumSize(300, 300)
self.setWindowTitle("Simple modelView")
self.show()
def on_item_changed(self, item):
print(item.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
This works fine. But I want to add extra arguments with 'itemChanged' signal. So, I've used lambda and functools
lambda
- changed from 'def on_item_changed(self, item)' to 'def on_item_changed(self, item, num)'
- changed from 'model.itemChanged.connect(self.on_item_changed)' to 'model.itemChanged.connect(lambda: self.on_item_changed(item, 1))'
- It has no error. But 'item.text()' shows only 'Toyota'. (maybe last item model)
functools.partial
- changed from 'def on_item_changed(self, item)' to 'def on_item_changed(self, item, num)'
- changed from 'model.itemChanged.connect(self.on_item_changed)' to 'model.itemChanged.connect(functools.partial(self.on_item_changed, item, 1))'
- It has no error. But 'item.text()' shows only 'Toyota'. (maybe last item model) Same result as lambda.
The questions are...
I don't know why lambda and functools shows wrong text.
Is any effective way to pass an extra args with signal?
Thank you for read my question.