PyQt5: get index of cell widgets and their chosen values
<p>I am using Python 3.5, and I have a simple code which creates a table with a few combo-boxes inside of it. Here is the code:</p>

<pre><code>from PyQt5 import QtCore
from PyQt5 import QtWidgets
import sys

app = QtWidgets.QApplication(sys.argv)

########################## Create table ##############################
tableWidget = QtWidgets.QTableWidget()
tableWidget.setGeometry(QtCore.QRect(200, 200, 250, 300))
tableWidget.setColumnCount(2)
tableWidget.setRowCount(9)
tableWidget.show()

################# Create tablecontent &amp; comboboxes ###################
names = ['Name 1', 'Name 2', 'Name 3', 'Name 4']
i = 0
for j in names:
    tableWidget.setItem(i, 0, QtWidgets.QTableWidgetItem(j))

    comboBox = QtWidgets.QComboBox()
    combo_option_list = ["Choose...", "Option 1", "Option 2", "Option 3", "Option 4"]
    for t in combo_option_list:
        comboBox.addItem(t)

    tableWidget.setCellWidget(i, 1, comboBox)
    i += 1

sys.exit(app.exec_()) 
</code></pre>

<p>I want to work with these combo-boxes, but I have several problems. It already starts with the fact that I can only use the last created combo-box with the following command:</p>

<pre><code>comboBox.activated[str].connect(do_something)
</code></pre>

<p>I need to know the following things:</p>

<ul>
<li>How to connect signals for all the combo-boxes?</li>
<li>How to get the index of the selected combo-box?</li>
<li>How to get the index of the selected combo-box option after it was chosen?</li>
</ul>

<p>The tricky part here is getting the index of the combo-box within the table, because a cell-widget cannot know its own index. One way to solve this is to use the <code>indexAt()</code> method of the table, which can get an index from a <code>QPoint</code>. A cell-widget <em>does</em> know its own position relative to its parent, so this can be passed to <code>indexAt()</code> to get its index within the table.</p>

<p>Below is a simple demo that shows how to connect up the signals and get all the information that you want:</p>

<pre><code>from PyQt5 import QtCore, QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.table = QtWidgets.QTableWidget(9, 2, self)
        layout = QtWidgets.QVBoxLayout(self)
        layout.addWidget(self.table)
        names = ['Name 1', 'Name 2', 'Name 3', 'Name 4']
        options = ["Choose...", "Option 1", "Option 2", "Option 3", "Option 4"]
        for index, name in enumerate(names):
            self.table.setItem(index, 0, QtWidgets.QTableWidgetItem(name))
            comboBox = QtWidgets.QComboBox()
            comboBox.activated.connect(self.comboActivated)
            comboBox.addItems(options)
            self.table.setCellWidget(index, 1, comboBox)

    def comboActivated(self, option):
        combo = self.sender()
        print('option: %s, "%s"' % (option, combo.itemText(option)))
        index = self.table.indexAt(combo.pos())
        print('row: %s, column: %s' % (index.row(), index.column()))

if __name__ == '__main__':

    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.setGeometry(600, 200, 300, 300)
    window.show()
    sys.exit(app.exec_())
</code></pre>

python pyqt signals-slots qtablewidget qcombobox