所以,我在 Maya 中使用 python 和 PySide2 来制作自定义工具。
我有一个模型(QTableModel)和一个 QTableView。
模型中的每一行都会有一堆信息和一个复选框。
我必须做一个 QItemDelegate 并将其用作复选框。那也行。我无法确定该代表是否已被选中。
我通过模型迭代获取其数据(将其存储在节点中的 Maya 场景中)
data = []
rows = self.rowCount(1) #self is the model in this snnipet
for row in range(rows):
array = []
for column in range (6): #6 for the fixed number of columns
info = index.data()
array.append(index.data())
data.append(array)
碰巧每行中的第一项是一个复选框(代表)。在最终的数据数组中,当我真的想获得它的状态时,我最终得到了实际的 QItemDelegate 对象,检查与否,但它没有 isChecked() 方法。
关于如何获得它的想法?
非常感谢!
######## 编辑 1因此,该模型确实具有评论中提到的标志:
def flags(self, index):
return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsUserCheckable
我认为我做错了代表,正如我承认的那样,我在网上找到了这个片段,我试图理解它......但它是这样的:
class CBDelegate(QItemDelegate):
def __init__(self, parent):
QItemDelegate.__init__(self, parent)
def paint(self, painter, option, index):
self.cb = QCheckBox()
try:
self.cb.setChecked(index.data())
except:
pass
if not self.parent().indexWidget(index):
self.parent().setIndexWidget(index, self.cb)
然后,在 TableView:
self.setItemDelegateForColumn(0, CBDelegate(self))
它有帮助吗?(如果你有 Maya 2017,我可以给你整个代码......这有点乱,因为我把它用作学习练习!
谢谢你。
############ 编辑 2表视图:
class Table(QTableView):
def __init__(self, *args, **kwargs):
QTableView.__init__(self, *args, **kwargs)
# Set the delegate for column 0 of our table
#self.setItemDelegateForColumn(6, ButtonDelegate(self)) #problem for another time
self.setItemDelegateForColumn(0, CBDelegate(self))
模型:
class Model(QtCore.QAbstractTableModel):
def __init__(self, cycles = [[]], headers = [], parent = None):
QtCore.QAbstractTableModel.__init__(self, parent)
self.cycles = cycles
self.headers = headers
def rowCount(self, parent):
return len(self.cycles)
def columnCount(self, parent):
return len(self.cycles[0])
def flags(self, index):
return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsUserCheckable
def data(self, index, role):
if role == QtCore.Qt.DisplayRole:
row = index.row()
column = index.column()
value = self.cycles[row][column]
return value
if role == QtCore.Qt.EditRole:
row = index.row()
column = index.column()
return self.cycles[row][column]
if (role == QtCore.Qt.TextAlignmentRole):
return QtCore.Qt.AlignCenter;
def setData(self, index, value, role = QtCore.Qt.EditRole):
change = False
if role == QtCore.Qt.EditRole:
row = index.row()
column = index.column()
value = value
if (column == 1) or (column == 4):
try:
str(value)
change = True
except:
pm.warning("Not a valid name")
change = False
elif (column == 2):
try:
int(value)
change = True
except:
pm.warning("Not a valid frame")
change = False
elif (column == 3):
try:
int(value)
change = True
except:
pm.warning("Not a valid frame")
change = False
elif (column == 5):
try:
int(value)
change = True
except:
pm.warning("Not a valid version number")
change = False
if change:
self.cycles[row][column] = value
self.dataChanged.emit(row, column)
return True
return False
def headerData(self, section, orientation, role):
if role == QtCore.Qt.DisplayRole:
if orientation == QtCore.Qt.Horizontal:
return self.headers[section]
def insertRows(self, position, rows, values = [] , parent = QtCore.QModelIndex()):
self.beginInsertRows(parent, position, position+rows-1)
self.cycles.insert(position, values)
self.endInsertRows()
self.getData()
def getData(self):
rows = self.rowCount(1)
data = []
for row in range(rows):
array = []
for column in range (6):
index = self.index(row, column)
info = index.data()
if type(info) == bool:
array.append(info)
elif type(info) == QItemDelegate:
val_is_checked = index.data(QtCore.Qt.CheckStateRole) != QtCore.Qt.Unchecked
array.append(val_is_checked)
else:
info = str(info)
array.append(info)
array.append("del")
data.append(array)
dic = {}
for item in data:
dic[item[1]]=item
for key in dic:
print key, dic[key]
#this from pickle
#newData = data2String(dic)
# and this is what I wanna save inside Maya
#pm.setAttr("cycleAnimationListNode.cycles", newData)
代表在上面的编辑 1 中。
然后我猜你仍然需要循环和标题来启动这个模型:
headers = ["Select", " Cycle Name ", " Start ", " End ", "Info", "Version", " Del "]
cycles = [[False,"idle","1","70","cool information","0", "deleteBtnPlaceHolder"]]
我希望这样做。
谢谢你。
##### 编辑 3我在模型中有这个自定义方法:
def getData(self):
rows = self.rowCount(1)
data = []
for row in range(rows):
array = []
for column in range (6):
index = self.index(row, column)
info = index.data()
array.append(info)
data.append(array)
dic = {}
for item in data:
dic[item[1]]=item
print ""
print "data:"
print ''
for key in dic:
print(key, dic[key])
我使用它将模型转换为字典,因此我可以将其序列化并存储为 Autodesk Maya 中节点的字符串属性。它运行正常,但它从第一列获得的信息始终为无。我必须以不同的方式检索它吗?