To set widgets on children items on QTreeView

眉间皱痕 提交于 2019-11-30 15:41:36

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!