QTreeView始终显示相同的数据



我有一个项目树。它是这样的:

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

问题是QTreeView总是用相同的信息来完成。我得到一个树状图,看起来像这样:

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

我在index()方法中放了一个"print",以查看是否正在创建索引,然后,当我第一次输入"General"类别时,会创建"Computadoras"one_answers"Tablets"的索引,但只是那一次!然后,显示的数据是错误的!有什么想法吗?

我给你我的实现树视图。什么等同于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()

来自Cristián pérez的评论

我看到我继承了QAbstractTABLEModel,而不是QAbstract ItemModel。当我改变这一点时,它起了作用。

相关内容

  • 没有找到相关文章

最新更新