To set widgets on children items on QTreeView

后端 未结 1 1772
野趣味
野趣味 2021-01-01 03:52

Thanks to this thread, I\'m able to add widgets to 2nd or later column of QAbstractItemView (in my example QTreeView) of top level items of a view.

1条回答
  •  隐瞒了意图╮
    2021-01-01 04:41

    Your mistake is on this line:

    qindex_widget_child = self._datamodel.index(n, 1, QModelIndex())
    self.setIndexWidget(qindex_widget_child, node_widget_child)
    

    It's giving you the index on row 2 column 1 of the model, which is the "2th item", not your child. Your child is on row 1 column 1 of std_item. In Qt, especially in QStandardItemModel, Children are stored relative to their parent, not relative to the model.

    I'm not familiar enough with PyQt to give you the exact code, but you should be able to do something like this:

    qindex_widget_child = std_item_child.index(QModelIndex())
    self.setIndexWidget(qindex_widget_child, node_widget_child)
    

    or like this:

    qindex_widget_child = std_item.child(0, 1).index(QModelIndex())
    self.setIndexWidget(qindex_widget_child, node_widget_child)
    

    0 讨论(0)
提交回复
热议问题