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>Description below 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 ###################

def dosomething(index):
    widget = app.sender()
    print('widget object:', widget)
    print('widget myname:', widget.my_name)
    print('widget index:', combo_list.index(widget))
    print('option index:', index)

#---------------------------------------------------------------------

names = ['Name 1', 'Name 2', 'Name 3', 'Name 4']
combo_option_list = ["Choose...", "Option 1", "Option 2", "Option 3", "Option 4"]

combo_list = []

for i, name in enumerate(names):

    tableWidget.setItem(i, 0, QtWidgets.QTableWidgetItem(name))

    combobox = QtWidgets.QComboBox()
    combobox.addItems(combo_option_list)
    combobox.my_name = name + ' (i=' + str(i) + ')'
    combobox.currentIndexChanged.connect(dosomething)

    combo_list.append(combobox)

    tableWidget.setCellWidget(i, 1, combobox)

#---------------------------------------------------------------------

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

<p>Inside <code>dosomething</code> you can use <code>app.sender()</code> to get widget which was clicked - and then you can use it to do something with this widget.</p>

<p>PyQt executes <code>dosomething</code> with argument - it is index of selected option in combobox.</p>

<p>To get index of <code>widget</code> in <code>combo_list</code> you can use standard list method <code>combo_list.index(widget)</code></p>

<p>But you can also create own variable in combobox (ie. <code>my_name</code>) to keep index or other value</p>

<pre><code>combobox.my_name = some_value
</code></pre>

<p>and later you can get in <code>dosomething</code></p>

<pre><code>value = widget.my_name
</code></pre>

<hr>

<p>BTW: </p>

<p>instead of <code>addItem(one_option)</code> and <code>for</code> loop you can use <code>addItems(option_list)</code> </p>

<p>You can use <code>enumerate(names)</code> and you will not need <code>i = 0</code> and <code>i += 1</code></p>

<pre><code>for i, j in enumerate(names):
</code></pre>

<p>You can create <code>combo_option_list</code> before <code>for</code> loop.</p>

python pyqt signals-slots qtablewidget qcombobox