How to get onClick a qt.QComboBox() in a table?

I am using qt.QTableWidget() with 2 columns. The first column contains checkbox, while the second column is a qt.QComboBox(). I want to get the event when the user select the item in the qt.QComboBox(). How should I achieve it with Slicer and python?

        for i in range(4):            
            checkbox = qt.QTableWidgetItem()
            checkbox.setCheckState(False)    
            self.table.setItem(i,0,checkbox)

            self.combo = qt.QComboBox()
            self.combo.addItem("Item 1")
            self.combo.addItem("Item 2")              
            self.combo.setCurrentIndex(0)
            self.table.setCellWidget(0, 1, self.combo)        

@lassoan: Could you please give me some hints to do it?

Hi John,

As this is a question about using Qt, I would suggest you become familar with the Qt documentation as it is really helpful for determining available signals that a widget might have. I’m recommending this method because this was how I was able to learn! Here’s the link to the signals for a QComboBox - https://doc.qt.io/archives/qt-5.10/qcombobox.html#signals. Other widgets can easily be searched with lots of helpful links on the various pages.

Once you know the type of signal to use then using it in python, as available through PythonQt, works like such:

def myIndexChanged(index):
  print("ComboBox index changed to {}".format(index))

combo = qt.QComboBox()
combo.addItems(["Item 1", "Item 2"])
combo.currentIndexChanged.connect(myIndexChanged)

Hopefully this example and information helps you get acquainted with using Qt from python :smile:

1 Like