You're calling set_selection before you call self.tree_view.setModel. I 
would guess that is the problem.
Also I think you've missed "index" from the parameters you're passing to 
view.select()

On Friday, 14 September 2018 09:25:40 UTC+10, kiteh wrote:
>
> I have the following QTreeView.
> I am trying to set the selection in the QTreeview based on the string I 
> have derived - eg. '/users/Alice/people' and so the highlighted cell should 
> only be 'people' that is listed under the Alice level.
>
> tree = {
> 'users': {
>     "John" : ["graphics"],
>     "Alice": ["book", "people"]
>     }
> }
>
> class MainWindow(QtGui.QMainWindow):
>     def __init__(self, parent=None):
>         super(MainWindow, self).__init__(parent)
>         self.tree_view = QtGui.QTreeView()
>         self.setCentralWidget(self.tree_view)
>         self.set_selection()
>
>         self.model = QtGui.QStandardItemModel()
>         self.populateTree(tree, self.model.invisibleRootItem())
>         self.tree_view.setModel(self.model)
>         self.tree_view.expandAll()
>
>     def populateTree(self, children, parent):
>         for child in children:
>             child_item = QtGui.QStandardItem(child)
>             parent.appendRow(child_item)
>             if isinstance(children, dict):
>                 self.populateTree(children[child], child_item)
>
>     def set_selection(self):
>         to_set = "/user/Alice/people"
>         view = self.tree_view.selectionModel()
>         index = self.model.indexFromItem(to_set)
>         view.select(QItemSelectionModel.Select|QItemSelectionModel.Rows)
>
>         
> win = MainWindow()
> win.show()
>
>
> Initially I thought that `findItems` may work but unfortunately that is 
> only for QTreeWidget.
> When I tried to do a `set_selection()`, I was prompted with an error that 
> says "# AttributeError: 'NoneType' object has no attribute 'select' # ", it 
> seems that the selectionModel is empty..
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/python_inside_maya/a6f2eb65-c598-40fb-98c6-c8f764becac5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to