Exemplo n.º 1
0
    def index(self, row, column, parent):
        """ Returns the index of the item in the model specified by the given row, column and parent index.
        row, column == int, parent == QModelIndex
        """

        if not self.hasIndex(row, column, parent):
            return QtCore.QModelIndex()

        if not parent.isValid():
            parentItem = self.root
        else:
            # So, here we go from QModelIndex to the actual object .. ?
            parentItem = parent.internalPointer()

        # the only place where a child item is queried
        childItem = parentItem.getChild(row)
        if childItem:
            # return self.createIndex(row, column)
            return self.createIndex(row, column, childItem)
            """
            # .. that one does not work for PySide 5.12+
            TypeError: 'PyQt5.QtCore.QAbstractItemModel.createIndex' called with wrong argument types:
            PyQt5.QtCore.QAbstractItemModel.createIndex(int, int, ServerListItem)
            Supported signatures:
            PyQt5.QtCore.QAbstractItemModel.createIndex(int, int, quintptr = 0)
            PyQt5.QtCore.QAbstractItemModel.createIndex(int, int, void = nullptr)
            """
        else:
            return QtCore.QModelIndex()
Exemplo n.º 2
0
    def parent(self, index):
        """ Returns the parent of the model item with the given index. If the item has no parent, an invalid QModelIndex is returned.
        """
        if not index.isValid():
            return QtCore.QModelIndex()

        childItem = index.internalPointer()
        # the only place where the parent item is queried
        parentItem = childItem.getParent()

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

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