TreeView多项选择在没有UI的情况下更改选择后无法正常工作

问题描述 投票:2回答:1

这可能是一个错误,尽管也许我误解了。

简要说明

[基本上,我发现在使用Gtk.TreeView更改选择后,使用[Shift +箭头]在Gtk.TreeSelection.select_iter中进行多项选择无法正常工作。另一方面,如果通过单击一行然后按“ Shift +箭头”来更改选择,则选择的行为将与预期的一样。

[我应该注意,如果通过调用Gtk.TreeSelection.select_iter来更改选定的行,则UI将按照您的期望进行更新,并且调用Gtk.TreeSelection.get_selected_rows()将返回其应行。只有当您随后尝试使用箭头键选择多行时,您才会得到奇怪的行为。

这可能是在这个自包含的示例中最好的说明,我已经尝试使其尽可能简单:

代码

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class TreeViewBug(Gtk.Window):

  def __init__(self):
    Gtk.Window.__init__(self)
    self.connect('destroy', Gtk.main_quit)

    # Create model consisting of row path and a name
    self.treeModel = Gtk.ListStore(int, str)
    self.treeModel.append([0, 'alice'])
    self.treeModel.append([1, 'bob'])
    self.treeModel.append([2, 'chad'])
    self.treeModel.append([3, 'dan'])
    self.treeModel.append([4, 'emma'])

    self.treeView = Gtk.TreeView()
    self.treeView.append_column(Gtk.TreeViewColumn('path', Gtk.CellRendererText(), text=0))
    self.treeView.append_column(Gtk.TreeViewColumn('name', Gtk.CellRendererText(), text=1))
    self.treeView.set_model(self.treeModel)

    # Allow for multiple selection
    self.treeView.get_selection().set_mode(Gtk.SelectionMode.MULTIPLE)

    self.add(self.treeView)

  def run(self):
    self.show_all()

    # Focus the TreeView so we can test multiple select via keyboard without clicking on a row
    self.treeView.grab_focus()

    # Manually change the selected row to the row with "chad"
    chadIter = self.treeModel[2].iter
    self.treeView.get_selection().select_iter(chadIter)

    print('Press "Shift+Down" and see what happens')
    print('  it should select "chad, dan", but instead it selects "bob, chad"')
    print('Afterwards, try clicking on "chad" and then pressing Shift+Down. It should behave normally')

    Gtk.main()

if __name__ == '__main__':
  tv = TreeViewBug()
  tv.run()

我尝试过的事情

我最初在代码中通过单击Gtk.TreeSelection.select_iter更改所选行以响应按钮单击时遇到错误。

我也尝试过:

  • 添加自定义选择功能(Gtk.TreeSelection.set_select_function
  • 在更改选择之前先清除选择(Gtk.TreeSelection.unselect_all
  • 异步更改选择(GLib.idle_add)。
  • 更改选择后重画TreeView

推测

我猜测TreeView / TreeViewSelection具有一些内部状态变量跟踪选择和行,由于某些原因,它们在调用TreeSelection.select_iter时未得到正确的更新。这些变量可能与UI功能有关,因为TreeSelection.get_selected_rows仍然可以正常工作。而且,由于多个选择的UI逻辑取决于先前的UI交互,因此UI需要附加状态信息也很有意义(Shift + Down在扩展选择时的行为会有所不同,具体取决于您最初是向上还是向下选择)

gtk3 pygobject
1个回答
1
投票

因为Gtk.TreeView使用MVC,所以实际上您需要设置treeview的光标。这可能会影响程序的其余部分,具体取决于您正在执行的操作。示例:

#chadIter = self.treeModel[2].iter
#self.treeView.get_selection().select_iter(chadIter)
path = 2
column = self.treeView.get_column(0)
edit = False
self.treeView.set_cursor(path, column, edit)
© www.soinside.com 2019 - 2024. All rights reserved.