QTreeWidget with custom QTreeWidgetItem

Often it’s needed to represent items in a row, where each row can have widgets related to it.
In this example we’ll create QTreeWidgetItem with a QSpinBox and QPushButton for each row.

This is a basic example, but can have many uses.

qt_treeWidget

3 thoughts on “QTreeWidget with custom QTreeWidgetItem

  1. Hi! I’m looking at your example here, but I don’t seem to be able to nest your CustomTreeItem items. I want to do something like:

    item = CustomTreeItem( self.treeWidget, “rock”)
    seconditem = CustomTreeItem( item, “paper” )

    To get a proper tree of those custom widgets.
    If I do that, I get:
    TypeError: QSpinBox(QWidget parent=None): argument 1 has unexpected type ‘CustomTreeItem’

    Since CustomTreeItem inherits from QtGui.QTreeWidgetItem, it seems to me like it ought to allow me to provide it as a parent in a tree, but I don’t know. Any ideas?

    Thanks!
    Ben

    • Hey Ben,

      sorry, my example wasn’t complete like you found out.
      I was passing parent where I should pass self.treeWidget().

      I’ve updated CustomTreeItem class with the correction.

      hope it help,
      Asi

  2. You can define new user types in QTreeWidgetItem subclasses to ensure that custom items are treated specially; for example, when items are sorted.

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha! *