I have a tree of items. It is like this:
Categorias (root)
- General
--- Computadoras
--- Tablets
- Insumos
--- Cartuchos
The problem is that the QTreeView
is being completed always with the same information. I get a tree view looking like this:
Categorias (root)
- General
--- General
--- Insumos
- Insumos
--- General
I have put a "print" in the index()
method in order to see if the index was being created, and then, when I enter, for example, the "General" category for the first time, indexes for "Computadoras" and "Tablets" are created, but just that time! And then, the displayed data is wrong! Any Idea?
I give you my implementation for the tree view. What is equal to the Qt tutorial one?
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()
From comment by Cristián Pérez
I saw that i was inheriting from QAbstractTABLEModel instead of QAbstractItemModel. When I changed that, it worked.