2

I have a TreeView and an array containing ModelIndexes from that tree. The treeview's selectionMode is set to Controls.SelectionMode.ExtendedSelection.

Is there a way to highlight all the rows in the tree based on the indexes in the array?

I can already select just one row using

myTreeView.selection.setCurrentIndex(myindex, ItemSelectionModel.ClearAndSelect)

but I could not find a way to select/highlight multiple rows?

Ehsan Enayati
  • 299
  • 2
  • 16

2 Answers2

2

I found a solution. I am not sure if that is the best way, but I will post it here in case someone also looked for such thing.

    myTreeView.selection.clear()

    for(var j = 0; j < selectedindex.length; j++)
   {
       myTreeView.selection.setCurrentIndex(selectedindex[j], ItemSelectionModel.Select)
   }

So the point was to use Select as ItemSelectionModel, not ClearAndSelect!

Ehsan Enayati
  • 299
  • 2
  • 16
-1
import tkinter as tk
import tkinter.ttk as ttk

class App(tk.Frame):

    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.CreateUI()
        self.grid(sticky = (tk.N,tk.S,tk.W,tk.E))
        parent.grid_rowconfigure(0, weight = 1)
        parent.grid_columnconfigure(0, weight = 1)

    def CreateUI(self):
        tv = ttk.Treeview(self,yscrollcommand=sc.set,height=30)
        tv['columns'] = ('Name',)
        tv.heading("#0", text='Items')
        tv.column("#0", anchor="w",width=75)
        tv.heading('Name', text='Name')
        tv.column('Name', anchor='w', width=150)
        tv.grid(sticky = (tk.N,tk.S,tk.W,tk.E))
        self.treeview = tv
        self.treeview.bind('<Control-Enter>',self.OnClick)

    def OnClick(self,event):
        rows = self.treeview.get_children()
        for row in rows:
            self.treeview.selection_add(row)
       
       



       
items=[]
for i in range(100):
    items.append([i,'Item %d' % i])

root=tk.Tk()
sv=tk.StringVar()
filt=tk.Entry(root,textvariable=sv)
filt.grid(row=0,column=0,sticky='nw')
sc=tk.Scrollbar(root)
sc.grid(row=1,column=1,sticky='ns')
item_list=App(root)
item_list.grid(row=1,column=0,sticky='ns')
sc.config(command=item_list.treeview.yview)
for i in range(len(items)):
    item_list.treeview.insert('', 'end', iid=str(i), text=items[i][0], values=(items[i][1],))
item_list.treeview.selection_set('0')

def update_filter(*args):
    global items,item_list,sv
    filtr=sv.get().lower()
    item_list.treeview.delete(*(item_list.treeview).get_children())
    for i in range(len(items)):
        if filtr in str(items[i][0]).lower() or filtr in str(items[i][1]).lower():
            item_list.treeview.insert('', 'end', iid=str(i), text=items[i][0], values=(items[i][1],))
    item_list.treeview.update()
    item_list.update()

sv.trace('w', update_filter)
filt.focus()
root.mainloop()
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 20 '21 at 01:05
  • The question is about qt, not tkinter. – Bryan Oakley Nov 21 '21 at 00:34