I have to create similar objects and connect certain actions on signals so I used a loop. I also want to pass the object as an argument to my signal handlers.
And while each QComboBox created is different, the lambda function always passes the last QComboBox as the argument. Regardless of which QComboBox emitted the signal.
Minimal Reproducible Example:
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
class PageComp(QWidget):
def __init__(self, *args, **kwargs):
super(PageComp, self).__init__(*args, **kwargs)
Toplayout = QGridLayout(self)
Equibox = QGridLayout()
self.Quipos = []
for i in range(2):
Equi = QComboBox()
self.Quipos.append(Equi)
Equi.highlighted.connect(lambda s: self.OnEquihover(Equi, s))
Equi.setMinimumWidth(50)
Equi.addItem("(null)")
Equi.addItem("testos")
Equi.addItem("testos2")
Equi.currentIndexChanged.connect(lambda s: self.OnItemselect(Equi, s))
Equibox.addWidget(Equi, i, 0, 1, 2) # adding to the layout
Toplayout.addLayout(Equibox, 0, 0, 2, 1)
def OnEquihover(self, wdgt: QComboBox, index):
print(wdgt)
print(index)
def OnItemselect(self, wdgt: QComboBox, Item):
print(wdgt)
print(Item)
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
Host = PageComp(self)
self.setCentralWidget(Host)
app = QApplication([])
fen = MainWindow()
fen.show()
app.exec_()