Items are not adding in correctly in QTableWidget in Maya
<p>I am trying to create a table, where it has 2 columns and several rows.
Column1 will be listing all the available mesh/geos in the scene while Column2 will be in the form of combo box per cell where it contains several options - depending on the mesh/geos from Column1, it will lists different shader options in the combobox as it will be reading off from a file. Meaning to say in the table, each item is on a per-row basis.</p>

<p>I am currently having issues with populating the list of mesh/geos into Column1. Suppose my scene has 5 geos - <code>pCube1, pCube2, pCube3, pCube4, pCube5</code>, in my table, I would be expecting the Column0 of its 5 rows to be populated with <code>pCube#</code>, however instead of that, I got <code>pCube5</code> as my output result instead.</p>

<p>Please see the following code:</p>

<pre><code>from PyQt4 import QtGui, QtCore
from functools import partial
import maya.cmds as cmds


class combo_box( QtGui.QComboBox ):
    # For combox
    def __init__( self, *args, **kwargs ):
        super( combo_box, self ).__init__( *args, **kwargs)

def get_all_geos():
    all_geos = cmds.ls(type='mesh')
    return all_geos


class TestTable( QtGui.QWidget ):
    def __init__( self, parent=None ):
        QtGui.QWidget.__init__( self, parent )

        self.setLayout( QtGui.QVBoxLayout() )
        self.resize( 600, 300 )

        self.myTable = QtGui.QTableWidget()
        self.myTable.setColumnCount( 2 )

        rowCount = len(get_all_geos())
        self.myTable.setRowCount(rowCount)    

        self.setTable()  

        self.layout().addWidget(self.myTable)
        self.myTable.cellChanged.connect(self.update) 

    def setTable(self):
        # Adding the list of mesh found in scene into first column
        for geo in get_all_geos():
            item = cmds.listRelatives(geo, parent=True)[0]
            for i in range(0, self.myTable.rowCount()):
                # instead of being populated with the list of items, I got the same name for the entire column
                self.myTable.setItem(i, 0, QtGui.QTableWidgetItem(item))

                # sets the combobox into the second column
                box = combo_box()
                nameList = ("test1","test2","test3")
                box.addItems(nameList)
                self.myTable.setCellWidget(i,1,box)
                box.currentIndexChanged.connect(partial(self.tmp, i))

    def tmp(self, rowIndex, comboBoxIndex):
        item = "item " + str(comboBoxIndex)
        self.myTable.setItem(rowIndex, 2, QtGui.QTableWidgetItem(item))



if __name__ == "__main__":
    tableView = TestTable()
    tableView.show()
</code></pre>

<p>In my <code>setTable</code> function, the <code>item</code> is not being processed correctly? when I am trying to add it into the QTableWidget. Can someone advise?</p>

<p>Additionally, if anyone could answers, does the format I have used, would it be applicable for the scenario I am trying to achieve as I mentioned at the start of the post?</p>

<p>In your <code>setTable()</code> method, you are looping through the geometries, then you are looping through the rows. Since each geometry represents a row you only really need to loop through them and remove the other loop.</p>

<p>Modifying it like so fixes the output:</p>

<pre><code>def setTable(self):
    # Adding the list of mesh found in scene into first column
    geos = get_all_geos()
    for i in range(0, len(geos)):
        item = cmds.listRelatives(geos[i], parent=True)[0]
        # instead of being populated with the list of items, I got the same name for the entire column
        self.myTable.setItem(i, 0, QtGui.QTableWidgetItem(item))

        # sets the combobox into the second column
        box = combo_box()
        nameList = ("test1","test2","test3")
        box.addItems(nameList)
        self.myTable.setCellWidget(i,1,box)
        box.currentIndexChanged.connect(partial(self.tmp, i))
</code></pre>

<p>The reason it was failing was because your second loop kept overriding the rows with the last geo in the list.</p>

python pyqt maya