0

Given a list of strings, I am trying to populate the items in a tree view. Here is my code:

class MyModel(QtGui.QStandardItemModel):
    def __init__(self, parent=None):
        super(MyModel, self).__init__(parent)
        self.get_contents()

    def get_contents(self):
        self.clear()
        contents = [
            '|Base|character|Mike|body',
            '|Base|character|John',
            '|Base|camera'
        ]

        for content in contents:
            count = content.count('|')
            for index in range(count):
                index = index + 2
                split_path = content.split('|')[0:index]
                self.add_item(split_path)

    def add_item(self,name):
        item1 = QtGui.QStandardItem(name)
        self.appendRow([item1])

However, the hierarchy that I have got in my Tree View are not collapsible (those with the small arrow icons by the side) and each row is appended with values and editable (if double click), in which I do not want.

An example of the output from my code:

|Base
|Base|character
|Base|character|Mike
|Base|character|Mike|body
|Base
|Base|character
|Base|character|John
|Base
|Base|camera

where there are a few repeatable rows...

And this is what I am expecting:

|-- Base
|--|-- character
|--|--|-- Mike
|--|--|--|-- body
|--|-- character
|--|--|-- John
|--|-- camera

Any insights?

1
  • @eyllanesc It will be pyqt4 Commented Sep 8, 2018 at 0:21

1 Answer 1

1

You have to add the children if this is not part of the children, also you must remove the first element of the result of the split() since it is an empty element:

from PyQt4 import QtCore, QtGui


class MyModel(QtGui.QStandardItemModel):
    def __init__(self, parent=None):
        super(MyModel, self).__init__(parent)
        self.get_contents()

    def get_contents(self):
        self.clear()
        contents = [
            '|Base|character|Mike|body',
            '|Base|character|John',
            '|Base|camera'
        ]

        for content in contents:
            parent = self.invisibleRootItem()
            for word in content.split("|")[1:]:
                for i in range(parent.rowCount()):
                    item = parent.child(i) 
                    if item.text() == word:
                        it = item
                        break
                else:
                    it = QtGui.QStandardItem(word)
                    parent.setChild(parent.rowCount(), it)
                parent = it


if __name__ == '__main__':
    import sys

    app = QtGui.QApplication(sys.argv)

    w = QtGui.QTreeView()
    model = MyModel(w)
    w.setModel(model)
    w.show()
    w.expandAll()
    sys.exit(app.exec_())

enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

Thank you, it works. Is it possible to change the sizing eg. making the items to appear bigger? And/ Or re-iterates each item in the QStandardItemModel again such that I will be re-getting back '|Base', '|Base|character' etc..?
@dissidia What do you mean by changing the size? Do you want to change the size of the items by changing the size of the font?
Yes, changing the font to make it bigger
Hey, sorry for jumping onto your thread. I am doing something similiar, in which I am trying to add in a refresh functionality. I tried using <model>.clear() and followed by re-calling the MyModel() which does not seems to work - The contents did get emptied and it stays blank all the way...
@yan Are you using QStandardItemModel?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.