Search code examples
pythonpython-3.xpysideqtreewidgetqtreewidgetitem

pyside/pyqt: when converting str() to QTreeWidgetItem() the str() is shortened to the [0] of str()


s = 'someString'
s = QTreeWidgetItem(s)
print(s.text(0))           # 0 being 'column'

Output:

's'

It also appears as 's' if I run 'addChild(s)' to another QTreeWidgetItem.


Solution

  • QTreeWidgetItem construct is meant to be passed multiple strings (not a single one):

    >>> s = QTreeWidgetItem(['someString', 'otherString'])
    >>> print(s.text(0))
    someString
    >>> print(s.text(1))
    otherString
    

    Passing a single string object 'someString' is like passing a sequence with multiple single-character strings ['s', 'o', 'm', 'e', 'S', 't', 'r', 'i', 'n', 'g'].

    If you want pass a single string, wrap in list or tuple:

    s = QTreeWidgetItem(['someString'])