QTreeView始终显示相同的数据

时间:2013-12-13 23:19:17

标签: qt pyside qtreeview qtgui qabstractitemmodel

我有一个项目树。就像这样:

Categorias (root)
- General
--- Computadoras
--- Tablets
- Insumos
--- Cartuchos

问题是QTreeView始终使用相同的信息完成。我看到这样的树视图:

Categorias (root)
- General
--- General
--- Insumos
- Insumos
--- General

我在index()方法中放了一个“print”,以查看是否正在创建索引,然后,当我第一次输入“常规”类别时,索引为“Computadoras”和“平板电脑”创建,但就在那个时候!然后,显示的数据是错误的!有什么想法吗?

我给你了树视图的实现。什么等于Qt教程?

def buildTree(categorias, parentTree, step):
    for categoria in categorias:
        #print "-"*step, categoria.descripcion
        newTreeItem = CategoriasTreeItem(categoria, parentTree)
        parentTree.appendChild(newTreeItem)
        if len(categoria.subCategorias) > 0:
            buildTree(categoria.subCategorias, newTreeItem, step + 1)


class CategoriasProductoTableModel(QtCore.QAbstractTableModel):
    def __init__(self, session):
        QtCore.QAbstractTableModel.__init__(self)
        self.session = session
        self.rootItem = CategoriasTreeItem()

    def updateData(self):
        categorias = self.session.query(CategoriaProducto).filter(CategoriaProducto.idCategoriaSuperior == None).all()
        #print [len(categoria.subCategorias) for categoria in categorias]
        buildTree(categorias, self.rootItem, 1)
        #c = self.rootItem.child(0)
        #print [i.data(0) for i in c.childItems]
        self.modelReset.emit()

    def setData(self, index, value, role):
        if index.isValid() and role == QtCore.Qt.EditRole:
            # implementar los seteos aca
            return True
        else:
            return False

    def columnCount(self, parent):
        if parent.isValid():
            return parent.internalPointer().columnCount()
        else:
            return self.rootItem.columnCount()

    def data(self, index, role):
        if not index.isValid():
            return None
        if role != QtCore.Qt.DisplayRole:
            return None

        item = index.internalPointer()
        return item.data(index.column())

    def flags(self, index):
        if not index.isValid():
            return QtCore.Qt.NoItemFlags

        return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable

    def headerData(self, section, orientation, role):
        if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
            return self.rootItem.data(section)

    def index(self, row, column, parent):
        #if row < 0 or column < 0 or row >= self.rowCount(parent) or column >= self.columnCount(parent):
        if not self.hasIndex(row, column, parent):
            return QtCore.QModelIndex()

        if not parent.isValid():
            parentItem = self.rootItem
        else:
            parentItem = parent.internalPointer()

        childItem = parentItem.child(row)
        if childItem:
            return self.createIndex(row, column, childItem)
        else:
            return QtCore.QModelIndex()

    def parent(self, index):
        if not index.isValid():
            return QtCore.QModelIndex()

        childItem = index.internalPointer()
        parentItem = childItem.parent()

        if parentItem == self.rootItem:
            return QtCore.QModelIndex()

        return self.createIndex(parentItem.row(), 0, parentItem)

    def rowCount(self, parent):
        if parent.column() > 0:
            return 0

        if not parent.isValid():
            parentItem = self.rootItem
        else:
            parentItem = parent.internalPointer()

        return parentItem.childCount()

1 个答案:

答案 0 :(得分:0)

来自CristiánPérez的评论

我看到我继承自QAbstractTABLEModel而不是QAbstractItemModel。当我改变它时,它起作用了。