I was looking at this code example and I can't understand how this lambda function works in this scenario.
from PySide6 import QtCore, QtGui, QtWidgets
class ButtonDel(QtWidgets.QStyledItemDelegate):
clicked = QtCore.pyqtSignal(QtCore.QModelIndex)
def paint(self, painter, option, index):
if (
isinstance(self.parent(), QtWidgets.QAbstractItemView)
and self.parent().model() is index.model()
):
self.parent().openPersistentEditor(index)
def createEditor(self, parent, option, index):
button = QtWidgets.QPushButton(parent)
button.clicked.connect(lambda *args, ix=index: self.clicked.emit(ix))
return button
def setEditorData(self, editor, index):
editor.setText(index.data(QtCore.Qt.DisplayRole))
def setModelData(self, editor, model, index):
pass
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = QtWidgets.QTableView()
model = QtGui.QStandardItemModel(0, 2)
for i in range(10):
it1 = QtGui.QStandardItem()
it1.setData(QtCore.Qt.Checked, QtCore.Qt.CheckStateRole)
it1.setFlags(it1.flags() | QtCore.Qt.ItemIsUserCheckable)
it2 = QtGui.QStandardItem()
it2.setData("text-{}".format(i), QtCore.Qt.DisplayRole)
model.appendRow([it1, it2])
# pass the view as parent
delegate = ButtonDel(w)
w.setItemDelegateForColumn(1, delegate)
def on_clicked(index):
print(index.data())
delegate.clicked.connect(on_clicked)
w.setModel(model)
w.show()
sys.exit(app.exec_())
I understand that lambda functions with any number of arguments can be written like this.
ex (lambda *args: sum(args))(1,2,3) = sum((1,2,3)) = 6
What does *args do? Since we don't have any arguments like above '(1,2,3)' what is its use here.
button.clicked.connect(lambda *args, ix=index: self.clicked.emit(ix))
if index is getting passed into this slot here:
def on_clicked(index):
print(index.data())
I tried to rewrite this lambda function as a regular function but it doesn't seem to have the same functionality. What would be the regular function implementation of it?