Hello! I've got a question about implementation of a standart tree model for TreeView.

I have a response from database that lools like

[A, [a,b,c]],

And I want to pass it through treemodel to treeview, for in GUI it looks like that:


I've took a look at example, that comes with PyQT4, but in that example they retrieve data from a text file, where tree structure is marked by tabs. and I cannot realize how I can modify this standart example for it works with my data...

I'll be thankful for any help...

Finally I've fount a solution!

(Idea taken from here: http://www.mail-archive.com/pyqt@riverbankcomputing.com/msg19400.html)

class TreeModel(QtCore.QAbstractItemModel):  
        def __init__(self, data, parent=None):  
            super(TreeModel, self).__init__(parent)  
            self.dbdata = data  
            self.rootItem = TreeItem([u"NameOfColumn"])  
            self.setupModelData(self.dbdata, self.rootItem)  
        def setData(self, index, value, role):  
           if index.isValid() and role == QtCore.Qt.EditRole:  
               prev_value = self.getValue(index)  
               item = index.internalPointer()  
               return True  
               return False  
       def removeRows(self, position=0, count=1,  parent=QtCore.QModelIndex()):  
           node = self.nodeFromIndex(parent)  
           self.beginRemoveRows(parent, position, position + count - 1)  
       def nodeFromIndex(self, index):  
           if index.isValid():  
               return index.internalPointer()  
               return self.rootItem  
       def getValue(self, index):  
           item = index.internalPointer()  
           return item.data(index.column())  
       def columnCount(self, parent):  
           if parent.isValid():  
               return parent.internalPointer().columnCount()  
               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 QtCore.QVariant(item.data(index.column()))  
      def flags(self, index):  
           if not index.isValid():  
               return QtCore.Qt.NoItemFlags  
           return QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEditable  
       def headerData(self, section, orientation, role):  
           if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:  
               return QtCore.QVariant(self.rootItem.data(section)[0])  
           return None  
       def index(self, row, column, parent):  
           if row < 0 or column < 0 or row >= self.rowCount(parent) or column >= self.columnCount(parent):  
               return QtCore.QModelIndex()  
           if not parent.isValid():  
               parentItem = self.rootItem  
               parentItem = parent.internalPointer()  
           childItem = parentItem.child(row)  
           if childItem:  
               return self.createIndex(row, column, childItem)  
               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  
               parentItem = parent.internalPointer()  
           return parentItem.childCount()  
       def setupModelData(self, lines, parent):  
           ind = []  
           numb = 0  
           for line in lines:  
               self.parents[-1].appendChild(TreeItem(lineData, self.parents[-1]))  
               columnData = line[1]  
               self.parents.append(self.parents[-1].child(self.parents[-1].childCount() - 1))  
               for j in columnData:  
                    self.parents[-1].appendChild(TreeItem(j, self.parents[-1]))  
               if len(self.parents) > 0:  
  class TreeItem(object):  
      def __init__(self, data, parent=None):  
          self.parentItem = parent  
          self.itemData = data  
          self.childItems = []  
      def appendChild(self, item):  
      def child(self, row):  
          return self.childItems[row]  
      def childCount(self):  
          return len(self.childItems)  
      def columnCount(self):  
          return len(self.itemData)  
      def data(self, column):  
              return self.itemData  
          except IndexError:  
              return None  
      def parent(self):  
          return self.parentItem  
      def row(self):  
          if self.parentItem:  
              return self.parentItem.childItems.index(self)  
          return 0  
      def setData(self, data):  
          self.itemData = data

Edited 6 Years Ago by annndrey: n/a

Now it works but I still don't know how to add a support for several columns in this model...

This question has already been answered. Start a new discussion instead.