Re: Editable SpinButton on a TreeView: detect *every* changes



Il 14/11/2016 18:19, Pozz Pozz ha scritto:
I created with success a TreeView based on a ListStore. One column has a SpinButton (actually a GtkCellRendererSpin).

Now I want to detect when the user *is* changing the value, i.e. every time he clicks on + or - buttons.

If I connect the "edited" signal, my callack will be called only when the spin button is "unfocused". I lost every clicks.


I have tried to catch "editing_started", "editing_canceled" and "edited" signals of CellRenderer. In "editing_started" callback, I retrieve and save the row and column of the cell the user is going to edit. I also connect a callback to "value_changed" signal of the GtkAdjustment associated to the GtkCellRendererSpin. Moreover I save the starting value so I can restore it if the user will cancel the editing.

When the user clicks on +/- buttons, "value_changed" is called (so I can send the request to the remote device).

With some tricks, I'm able to restore the original value if the user cancels the editing and to avoid a duplicate request to the remote device when the user confirm the value changed by +/- buttons (actually when the user moves the focus away from the spinbutton, thus terminated the editing).

As you know, I'm very new to Gtk programming, so I'm wondering wethere there's a better approach.

Here it is my code in Python:

    def on_cellspin_table_editing_started(self, renderer, editable, path):
        print("editing_started")
        self.index_of_table = int(path)
for column_idx, column in zip(range(len(self.tv_calib.get_columns())), self.tv_calib.get_columns()):
            if column.get_cells()[0] is renderer:
                self.table_column = column_idx
                break
        self.table_adj = editable.get_adjustment()
self.start_value = self.liststore[self.index_of_table][self.table_column]
        self.table_editing = True
        self.table_adj.connect("value-changed", self.on_adj_value_changed)

    def on_cellspin_table_edited(self, renderer, path, new_text):
        print("edited")
        self.table_editing = False
self.table_adj.disconnect_by_func(self.on_adj_value_changed)
        new_value = int(new_text)
        if int(self.table_adj.get_value()) != new_value:
            self.set_value(self.index_of_table, new_value)

    def on_cellspin_table_editing_canceled(self, renderer):
        print("editing_canceled")
        self.table_editing = False
self.table_adj.disconnect_by_func(self.on_adj_value_changed)
        if self.start_value != int(self.table_adj.get_value()):
self.set_value(self.index_of_table, self.table_column, self.start_value)

    def on_adj_value_changed(self, adj):
        print("value_changed")
self.set_value(self.index_of_table, self.table_column, int(adj.get_value()))



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]