0

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_()
User54869
  • 11
  • 2
  • please provide a [mcve] – S. Nick Mar 29 '21 at 19:32
  • I provided a fast-made minimal reproducible example while maintaining a somewhat readable architecture. It should print the widget passed on both events and according to my issue, it is the last one regardless of the widget emitting. I could reduce the size of the example but I'm low on time in my timezone I'll do it in a later edit. – User54869 Mar 29 '21 at 20:00

1 Answers1

0

Try it:

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.highlighted.connect(lambda ind, w=Equi: self.onEquihover(ind, w))
#            Equi.currentIndexChanged.connect(lambda s: self.OnItemselect(Equi, s))
            Equi.currentIndexChanged.connect(lambda ind, w=Equi: self.onItemselect(ind, w))

            Equibox.addWidget(Equi, i, 0, 1, 2)  # adding to the layout

        Toplayout.addLayout(Equibox, 0, 0, 2, 1)

#    def OnEquihover(self, wdgt: QComboBox, index):
    def onEquihover(self, index, wdgt: QComboBox):
        print(f'onEquihover: {index}, {wdgt.itemText(index)}')
#        print(index)

#    def OnItemselect(self, wdgt: QComboBox, Item):
    def onItemselect(self, index, wdgt: QComboBox):
        print(f'OnItemselect: {index}, {wdgt.itemText(index)}')


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_()

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33