我正在尝试创建一个表,它有2列和几行。 Column1将列出场景中所有可用的网格/地理,而Column2将采用每个单元格的组合框形式,其中包含多个选项 - 根据Column1的网格/地理位置,它将在组合框中列出不同的着色器选项它将从文件中读取。在表格中说的是,每个项目都是按行进行的。
我目前在填充网格/地理列表到Column1时遇到问题。假设我的场景有5个地理位置 - pCube1, pCube2, pCube3, pCube4, pCube5
,在我的表格中,我希望其中5行的Column0可以填充pCube#
,但不是这样,我得到了pCube5
我的输出结果。
请参阅以下代码:
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()
在我的setTable
函数中,item
未正确处理?当我试图将它添加到QTableWidget时。有人可以建议吗?
此外,如果有人可以回答,我使用的格式是否适用于我试图实现的方案,就像我在帖子开头提到的那样?
答案 0 :(得分:1)
在setTable()
方法中,循环遍历几何,然后循环遍历行。由于每个几何表示一行,您只需要遍历它们并删除另一个循环。
修改它就像修复了输出:
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))
失败的原因是因为你的第二个循环一直覆盖列表中最后一个地理位置的行。