そのため、カスタム ツールには 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 内のノードの文字列アトリビュートとして保存できます。問題なく実行されますが、最初の列から取得する情報は常に None です。別の方法で取得する必要がありますか?