QItemDelegate CheckBox

时间:2017-12-30 01:08:23

标签: python maya qitemdelegate pyside2

所以,我在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

我认为我做的代表错了,正如我承认的那样,我发现这个snnipet在线并且我试图理解它......但它是这样的:

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])

我使用它将模型转换为字典,因此我可以将其序列化并作为Node的字符串属性存储在Autodesk Maya中。它运行正常,但从第一列获得的信息始终为None。我必须以不同的方式检索它吗?

1 个答案:

答案 0 :(得分:1)

如果您想要显示QCheckBox,则无需使用代理,您只需要正确启用标记Qt::ItemIsUserCheckable,此外您必须通过setData()保存信息,因为在您的代码中信息丢失,因为以下部分中存储的信息显示了具有这些修改的模型:

<强>更新

以前禁用第一列的角色Qt::DisplayRole的数据方法的返回,因为它将文本放在表中,这对我来说是不必要的,现在我已经启用了它,但是文本不是显示我已经放置了一个删除文本的委托

from PySide2.QtWidgets import *
from PySide2.QtCore import *

class Model(QAbstractTableModel):
    def __init__(self, cycles = [[]], headers = [], parent = None):
        QAbstractTableModel.__init__(self, parent)
        self.cycles = cycles
        self.headers = headers
        self.values_checked = []

    def rowCount(self, parent):
        return len(self.cycles)

    def columnCount(self, parent):
        return len(self.cycles[0])

    def flags(self, index):
        fl = Qt.ItemIsEnabled | Qt.ItemIsSelectable 
        if index.column() == 0:
            fl |= Qt.ItemIsUserCheckable
        else:
            fl |= Qt.ItemIsEditable
        return fl

    def data(self, index, role):
        if not index.isValid():
            return 
        row = index.row()
        column = index.column()

        if role == Qt.DisplayRole:
            value = self.cycles[row][column]
            return value

        elif role == Qt.TextAlignmentRole:
            return Qt.AlignCenter;

        elif role == Qt.CheckStateRole and column==0:
            return Qt.Checked if self.cycles[row][column] else Qt.Unchecked


    def setData(self, index, value, role = Qt.EditRole):
        change = False
        row = index.row()
        column = index.column()

        if role == Qt.CheckStateRole:
            value =  value != Qt.Unchecked
            change = True
        if role == Qt.EditRole:
            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 == Qt.DisplayRole:
            if orientation == Qt.Horizontal:
                return self.headers[section]

    def insertRows(self, position, rows, values = [] , parent =QModelIndex()):
        self.beginInsertRows(parent, position, position+rows-1)
        self.cycles.insert(position, values)
        self.endInsertRows()
        self.getData()

    def roleNames(self):
        roles = QAbstractTableModel.roleNames(self)
        roles["Checked"] = Qt.CheckStateRole
        return roles


    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])


class EmptyDelegate(QStyledItemDelegate):
    def paint(self, painter, option, index):
        opt = QStyleOptionViewItem(option)
        self.initStyleOption(opt, index)
        opt.text = ""
        QApplication.style().drawControl(QStyle.CE_ItemViewItem, opt, painter)


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    w = QTableView()
    w.setItemDelegateForColumn(0, EmptyDelegate(w))
    headers = ["Select", "  Cycle Name  ", " Start ", "  End  ", "Info", "Version", " Del "]
    cycles = [[True,"idle","1","70","cool information","0", "deleteBtnPlaceHolder"]]
    model = Model(cycles, headers)
    w.setModel(model)
    w.show()
    sys.exit(app.exec_())