select element automatically within QTreeView based model - python

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)

Related

Odoo v14: how can I get parent value in the creation of child?

Model is product.category, want to inherit and add a field level to add the levels to find which level record I want.
We knew that by default (as recommended) that first entry *All* here should present to start the parent child relations.
When the user creates a new category and select All as its parent, we knew that its level is 1, and we can add 2, programmatically it should be parent_level + 1 ... so we can add levels of any depth.
I don't know how to code it, so please help me for the below onchange function (or any other better approach, please specify), guide/modify it to achieve what I want to have.
#api.onchange(parent_id):
def _onchange_parent_id(self):
find the level of parent here...
self.level = parent_level + 1
not sure if i understand your question correctly, but if you just want a level to your category and provided the parent field name is parent_id:
level = fields.Integer('Level', compute='_compute_level')
#api.depends('parent_id')
def _compute_level(self):
for record in self:
record.level = (record.parent_id.level or 0) + 1

QFormLayout drops the label text when using setItem

Using below code to add Items into QFormLayout, but nothing displayed and yet no any error logs can be found.
fl1 = QFormLayout()
label = QLabel()
label.setText("HELLO")
item1 = QWidgetItem(label)
fl1.setItem(1, 0, item1)
item2 = QWidgetItem(QLineEdit())
fl1.setItem(1, 1, item2)
fl1.update()
fl1.invalidate()
Per the pyqt doc, I didn't yet find any specific note about setItem:
QFormLayout.setItem (self, int row, ItemRole role, QLayoutItem item)
The item argument has it's ownership transferred to Qt.
Sets the item in the given row for the given role to item, extending the layout with empty rows if necessary.
If the cell is already occupied, the item is not inserted and an error message is sent to the console. The item spans both columns.
Warning: Do not use this function to add child layouts or child widget items. Use setLayout() or setWidget() instead.
See also setLayout().
Please share any comments will be appreciated. Thanks!

Gtk.Treeview get current row index in Python

How to get index of current selected row in Gtk.TreeView in Python ?
The other answer is likely more useful in general. However, to answer the actual question posed by the OP, how to get the row index: assuming one row is selected, you can get it with:
index = treeview.get_selection().get_selected_rows()[1][0][0]
You can call gtk.TreeView.get_selection to get the current selection (gtk.TreeSelection). You can then call gtk.TreeSelection.get_selected to get:
a 2-tuple containing a reference to the gtk.TreeModel and a gtk.TreeIter pointing to the currently selected node.
The iter can be used on a gtk.TreeModel (which is obtained by calling gtk.TreeView.get_model. You can then use gtk.TreeModel.get_value to get any of the column values of the node at that position in the tree.
Somewhat late, perhaps not so relevant, but if you have double clicked on the selected row you will get the row index as element of the TreePath tuple, like so
def on_row_activated(self, treeview, path, column):
model = self.treeview.get_model()
tree_iter = model.get_iter(path)
row = path[0]
if tree_iter:
# here you can get the values of the columns

python ttk treeview: how to select and set focus on a row?

I have a ttk.Treeview widget with some rows of data. How do I set the focus to and select (highlight) a specified item?
tree.focus_set()
does nothing
tree.selection_set(0)
complains that: Item 0 not found, although the widget is plainly populated with more than zero items. Trying item 1 does no better.
EDIT: to select an item, find its id, then use tree.selection_set(id). Neither tree.focus(id) nor tree.focus_set(id) appears to do anything.
Get the id of treeview item you want to highlight/select
child_id = tree.get_children()[-1] # for instance the last element in tuple
To highlight the item, use both focus() and selection_set(item_id)
tree.focus(child_id)
tree.selection_set(child_id)
Note: I haven't worked with python.
Looking at this link, the focus method with optional parameter item, should highlight the node.
If not, look at selectmode option & set it to "browse".
Come across this question when I'm looking to solve the exact same problem.
Found out this:
tree.selection_set(item) highlights the item
tree.focus(item) or tree.focus_set(item) selects the item
def mycallback(event):
_iid = treeview.identify_row(event.y)
global last_focus
if _iid != last_focus:
if last_focus:
treeview.item(last_focus, tags=[])
treeview.item(_iid, tags=['focus'])
last_focus = _iid
treeview.tag_configure('focus', background='red')
global last_focus
last_focus = None
treeview.bind("<Motion>", mycallback)
Use
tree.selection_add(item_iid)
The reason why
tree.selection_set(0) doesn't work is because 0 is not the item iid, it's the index you're referring to and Treeview is expecting an iid.

How to get value from selected item in treeview in PyGTK?

I'm learning PyGtk. I have a simple treeview with 1 column, I get items for that treeview from list.
How to get value of selected item in treeview?
You may use the gtk.TreeView.get_selection() method to get the gtk.TreeSelection.
Next, you should use the gtk.TreeSelection.get_selected_rows() method to get the TreeModel (the ListStore) and the selected items
paths.
Then, you can use the gtk.TreeModel.get_iter() in order to get the iter from the path (returned by the gtk.TreeSelection.get_selected_rows() method).
Finally, you may use the gtk.TreeModel.get_value() method to get the value corresponding to the column and the iter previously recovered.
Example :
def onSelectionChanged(tree_selection) :
(model, pathlist) = tree_selection.get_selected_rows()
for path in pathlist :
tree_iter = model.get_iter(path)
value = model.get_value(tree_iter,0)
print value
listStore = gtk.ListStore(int)
treeview = gtk.TreeView()
treeview.set_model(listStore)
tree_selection = treeview.get_selection()
tree_selection.set_mode(gtk.SELECTION_MULTIPLE)
tree_selection.connect("changed", onSelectionChanged)

Categories