I have tried the example from the Qt for Python documentation which is here: https://doc.qt.io/qtforpython/examples/example_widgets_itemviews_editabletreemodel.html#editable-tree-model-example
After executing the example, I added some rows and child items as below: enter image description here
Now, I am willing to select the Country automatically so that they can be highlighted one by one from the code but not by the user. I tried as follow:
selection_model = self.treeView.selectionModel()
selection_model.select(arg1, arg2)
Now here in select)() method requires 2 arguments. could be like below:
select(index: QModelIndex | QPersistentModelIndex, command: SelectionFlag) -> None
select(selection: QItemSelection, command: SelectionFlag) -> None
My question is, how I can create the QModelIndex or QItemSelection and SelectionFlag objects to select the item from the code?
My Second question is, if I want to change the Tree data dynamically, for example want to change City from 'Toronto' to 'Hamilton', is there any api to do that without re-drawing the whole tree?
Thank you for your help. let me know is any more details required in my question.
Got the answer from the same example that I have linked in my question. You can select(highlight) any item within tree by using: (To select first child within the root item)
# selects root
index: QModelIndex = selection_model.currentIndex()
selection_model.setCurrentIndex(self._standard_model.index(0, 0, index), QItemSelectionModel.SelectionFlag.ClearAndSelect)
# selects first child within root
index: QModelIndex = selection_model.currentIndex()
selection_model.setCurrentIndex(self._standard_model.index(0, 0, index), QItemSelectionModel.SelectionFlag.ClearAndSelect)
And you can edit the Tree item values by code using:
index: QModelIndex = self.model.index(1, 1, QModelIndex()) # to edit value at (1, 1)
model: QAbstractItemModel = self.view.model()
model.setData(index, "[No data]", Qt.EditRole)