简体   繁体   中英

Synchoronize editable Gtk.TreeView with its ListStore

I'm building an interface in Gtk3/Python3, using Glade, showing data from a ListStore in a TreeView, and letting the user edit the values in one of the columns.

As said, I'm using glade, and in the glade file it's written which ListStore column serves what data for each GtkCellRenderer . The first funny thing is that I do not know how to get to this information, and I'm introducing redundancy in order to keep things working.

Next more interesting trouble: when an edited cell loses focus, but focus stays in the TreeView, my callback for the edited signal is invoked and I can store the edited value in the ListStore.

When an edited cell loses focus, and focus goes to a different widget (like in the further useless Button in the example), the registered callback is not invoked. Also if the window loses focus, the previous value is restored. This last one is particularly unpractical in the case 'focus-follows-mouse'.

What's the deal?

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


class TestWindow:

    def __init__(self, builder):
        import os.path
        path, name = os.path.split(__file__)
        builder.add_from_file(os.path.join(path, "treeedit.glade"))
        builder.connect_signals(self)
        self.window = builder.get_object("window1")
        self.window.connect("destroy", Gtk.main_quit)
        self.notes_list = builder.get_object("notes_list")
        self.notes_list.append(('a',))
        self.notes_list.append(('b',))
        self.notes_list.append(('c',))
        self.notes_list.append(('d',))

    def on_cell_edited(self, widget, path, text):
        self.notes_list[path][0] = text

    def show_all(self):
        self.window.show_all()


builder = Gtk.Builder()
win = TestWindow(builder)
win.show_all()
Gtk.main()

and this is the glade file:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkListStore" id="notes_list">
    <columns>
      <!-- column-name content -->
      <column type="gchararray"/>
    </columns>
  </object>
  <object class="GtkDialog" id="window1">
    <property name="can_focus">False</property>
    <property name="type_hint">dialog</property>
    <child internal-child="vbox">
      <object class="GtkBox">
        <property name="orientation">vertical</property>
        <property name="spacing">2</property>
        <child>
          <object class="GtkButton">
            <property name="label" translatable="yes">button</property>
          </object>
        </child>
        <child>
          <object class="GtkFrame">
            <property name="visible">True</property>
            <child>
              <object class="GtkAlignment">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="left_padding">12</property>
                <child>
                  <object class="GtkTreeView" id="notes_tree">
                    <property name="visible">True</property>
                    <property name="can_focus">True</property>
                    <property name="hexpand">True</property>
                    <property name="vexpand">True</property>
                    <property name="model">notes_list</property>
                    <property name="headers_clickable">False</property>
                    <child internal-child="selection">
                      <object class="GtkTreeSelection"/>
                    </child>
                    <child>
                      <object class="GtkTreeViewColumn">
                        <property name="resizable">True</property>
                        <property name="title" translatable="yes">value</property>
                        <child>
                          <object class="GtkCellRendererText" id="prefs_cellrenderertext1">
                            <property name="editable">True</property>
                            <signal name="edited" handler="on_cell_edited" swapped="no"/>
                          </object>
                          <attributes>
                            <attribute name="text">0</attribute>
                          </attributes>
                        </child>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
            <child type="label">
              <object class="GtkLabel">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">Notes</property>
              </object>
            </child>
          </object>
        </child>
      </object>
    </child>
  </object>
</interface>

My own answer is just a partial answer, because what if I need to edit multiple columns? as of now, I've resorted to writing multiple callbacks to the editing-started signal, like this:

def on_cell_editing_started_col0(self, *args):
    self.column = 0

def on_cell_editing_started_col1(self, *args):
    self.column = 1

then I'm using the self.column value in both callbacks, for Gtk.CellRendererText edited signal, and Gtk.TreeView set-focus-child signal.

Looks like I can register a callback to the signal set-focus-child for the container TreeView . This gets called when an editable child starts being edited, and when editing is finished. In both cases the callback receives two parameters, the first of which is the TreeView itself.

When editing starts, the second parameter is a temporary Gtk.Entry , with which the user interacts. When editing ends, the second parameter is a None .

I might use this code, and I'm sure I do not know how robust this might be:

def on_focus_child(self, tree, entry):
    if entry is not None:
        self.last_entry = entry
    else:
        tv, path = tree.get_selection().get_selected()
        self.notes_list[path][0] = self.last_entry.get_text()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM