0

This question has been asked before at:

https://stackoverflow.com/questions/26538667/pyqt-populate-qtreeview-from-txt-file-that-contains-file-paths

But didn't seem to get a response.

I have a dataset of file paths that are formatted, like so:

hon_dev/Bob Dylan/Concept
hon_dev/Andromeda/Modeling
hon_dev/Andromeda/Lookdev
hon_dev/Andromeda/Rigging
hon_dev/Andromeda/Animation
hon_dev/Andromeda/FX
hon_dev/fsafasfas/production
hon_dev/Magebane: Acheron of Mana Aeacus/Model
hon_dev/Magebane: Acheron of Mana Aeacus/Concept
hon_dev/Magebane: Acheron of Mana Aeacus/Texture
hon_dev/Skrull/Modeling
hon_dev/Skrull/Lookdev
hon_dev/Skrull/Rigging
hon_dev/Skrull/Animation
hon_dev/Skrull/FX
hon_dev/Bob Mylan/Modeling
hon_dev/Bob Mylan/Lookdev
hon_dev/Bob Mylan/Rigging
hon_dev/Bob Mylan/Animation
hon_dev/Bob Mylan/FX
hon_dev/Handsome Man/Concept
hon_dev/Handsome Man/Modeling
hon_dev/Handsome Man/Lookdev
hon_dev/Handsome Man/Rigging
hon_dev/Handsome Man/Animation
hon_dev/Handsome Man/FX
demo-sync/Drone Craft/Modelling Drone Craft
demo-sync/Drone Craft/Texturing and Shading of Drone Craft
demo-sync/Drone Craft/Rigging Drone Parts

And I'm trying to get them to fill up a QTreeView (PySide). The current code I have is as such, with a simple recursive function:

def doIt(self):

    self.model = QtGui.QStandardItemModel()

    # self.model.setHorizontalHeaderLabels = ['test']

    topLevelParentItem = self.model.invisibleRootItem()

    # create all itewms first



    # iterate over each string url
    for item in data:
        splitName = item.split('/')

        # first part of string is defo parent item
        # check to make sure not to add duplicate
        if len(self.model.findItems(splitName[0], flags=QtCore.Qt.MatchFixedString)) == 0:

            parItem = QtGui.QStandardItem(splitName[0])
            topLevelParentItem.appendRow(parItem)


        def addItems(parent, elements):

            # check if not reached last item in the list of items to add
            if len(elements) != 0:

                print "currently eval addItems({0}, {1}".format(parent.text(), elements)

                # check if item already exists, if so do not create 
                # new item and use existing item as parent
                if len(self.model.findItems(elements[0], flags=QtCore.Qt.MatchFixedString)) == 0:

                    print "item being created for {0}".format(elements[0])
                    item = QtGui.QStandardItem(elements[0])

                else:
                    print "not adding duplicate of: {0}".format(elements[0])
                    item = self.model.findItems(elements[0], flags=QtCore.Qt.MatchFixedString)[0]
                    print "the item to act as non-duplicate is: {0}".format(item.text())

                child = elements[1:]
                print "child is {0}".format(child)

                # call recursive function to add
                addItems(item, child)

                print "parenting: {0} to {1}".format(item.text(), parent.text())
                parent.appendRow(item)

        addItems(parItem, splitName[1:])



        print 'done: ' + item + '\n'



    self.inst.col_taskList.setModel(self.model)

However, because I can't find any way to look through a QStandardItem for existing rows, I'm getting this in the UI as a result:

enter image description here

Is there a way to find duplicates rows in a QStandardItem or traverse the QStandardItemModel to find the existing QStandardItem? I've been struggling with this problem for the past 2 days and trying to find an existing example, and I can't really wrap my head around how this could be such a complication...

Any help/advice on this would be appreciated! Thanks!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
sonictk
  • 178
  • 2
  • 10

1 Answers1

1

Hmm, after a bit of faffing about, I've come up with something that works for now, though the file paths must be in order for this to work:

def doIt(self):

    print "\n\n\n\n"

    self.model = QtGui.QStandardItemModel()

    topLevelParentItem = self.model.invisibleRootItem()

    # iterate over each string url
    for item in data:
        splitName = item.split('/')

        # first part of string is defo parent item
        # check to make sure not to add duplicate
        if len(self.model.findItems(splitName[0], flags=QtCore.Qt.MatchFixedString)) == 0:

            parItem = QtGui.QStandardItem(splitName[0])
            topLevelParentItem.appendRow(parItem)


        def addItems(parent, elements):
            """
            This method recursively adds items to a QStandardItemModel from a list of paths.
            :param parent:
            :param elements:
            :return:
            """

            for element in elements:

                # first check if this element already exists in the hierarchy
                noOfChildren = parent.rowCount()

                # if there are child objects under specified parent
                if noOfChildren != 0:
                    # create dict to store all child objects under parent for testing against
                    childObjsList = {}

                    # iterate over indexes and get names of all child objects
                    for c in range(noOfChildren):
                        childObj = parent.child(c)
                        childObjsList[childObj.text()] = childObj

                    if element in childObjsList.keys():
                        # only run recursive function if there are still elements to work on
                        if elements[1:]:
                            addItems(childObjsList[element], elements[1:])

                        return

                    else:
                        # item does not exist yet, create it and parent
                        newObj = QtGui.QStandardItem(element)
                        parent.appendRow(newObj)

                        # only run recursive function if there are still elements to work on
                        if elements[1:]:
                            addItems(newObj, elements[1:])

                        return

                else:
                    # if there are no existing child objects, it's safe to create the item and parent it
                    newObj = QtGui.QStandardItem(element)
                    parent.appendRow(newObj)

                    # only run recursive function if there are still elements to work on
                    if elements[1:]:
                        # now run the recursive function again with the latest object as the parent and
                        # the rest of the elements as children
                        addItems(newObj, elements[1:])

                    return

        # call proc to add remaining items after toplevel item to the hierarchy
        print "### calling addItems({0}, {1})".format(parItem.text(), splitName[1:])
        addItems(parItem, splitName[1:])

        print 'done: ' + item + '\n'

    self.inst.col_taskList.setModel(self.model)
sonictk
  • 178
  • 2
  • 10