kiteh
2018-09-13 23:25:40 UTC
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..
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 python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/b70070bc-63f6-436a-ad14-1875c13c03f5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
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 python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/b70070bc-63f6-436a-ad14-1875c13c03f5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.