How to detect treeview parent and child row on start edit and complete edit

Hi

I have a treeview entries like this

Parent 0
|-> Child 0
|-> Child 1
|-> Child 2
|-> Child 3
Parent 1
|-> Child 0
|-> Child 1
Parent 2
|-> Child 0
|-> Child 1
|-> Child 2
|-> Child 3
|-> Child 4

I have a class for tree store like this

#include <gtkmm.h>

class TreeModel_Dnd : public Gtk::TreeStore
{
protected:
  TreeModel_Dnd();

public:
  //Tree model columns:
  class treeModelCol : public Gtk::TreeModel::ColumnRecord
  {
  public:
    Gtk::TreeModelColumn<Glib::ustring> col_name;

    treeModelCol()
    { 
      add(col_name);
    }

  };

  treeModelCol treeColumn;

  static Glib::RefPtr<TreeModel_Dnd> create();

  using onTreeviewRowDragged = sigc::signal<void>;
  onTreeviewRowDragged onTreeviewRowDragged_Fun();


protected:
  //Overridden virtual functions:
  bool row_draggable_vfunc(const Gtk::TreeModel::Path& path) const override;
  onTreeviewRowDragged onTreeviewRowDragged_Sig;

};

I call this in the other class

  Glib::RefPtr<TreeModel_Dnd> treeViewPointer  
  
  Gtk::TreeModel::Row row;

  row = *(treeViewPointer->append());
  row[treeViewPointer->treeColumn.col_name] = "Parent 0";
  row = *(treeViewPointer->append());
  row[treeViewPointer->treeColumn.col_name] = "Parent 1";
  row = *(treeViewPointer->append());
  row[treeViewPointer->treeColumn.col_name] = "Parent 2";
  row = *(treeViewPointer->append());
  row[treeViewPointer->treeColumn.col_name] = "Parent 3";
  row = *(treeViewPointer->append());
  row[treeViewPointer->treeColumn.col_name] = "Parent 4";
  row = *(treeViewPointer->append());
  row[treeViewPointer->treeColumn.col_name] = "Parent 5";
  row = *(treeViewPointer->append());
  row[treeViewPointer->treeColumn.col_name] = "Parent 6";
  treeView->append_column("",treeViewPointer->treeColumn.col_name);

According to TreeView - Editable Cells

I have to create this

  Gtk::TreeView::Column treeviewColumnValidated;
  Gtk::CellRendererText cellRendererValidated;

  treeviewColumnValidated.pack_start(cellRendererValidated);
  treeView->append_column(treeviewColumnValidated);
  // Tell the view column how to render the model values:
  treeviewColumnValidated.set_cell_data_func(cellRendererValidated,sigc::mem_fun(*this,&MyClass::treeviewColumnValidatedOnCellData));

  // Make the CellRenderer editable, and handle its editing signals:
  cellRendererValidated.property_editable() = true;

  cellRendererValidated.signal_editing_started().connect(sigc::mem_fun(*this,&MyClass::cellRendererValidatedOnEditingStarted));

  cellRendererValidated.signal_edited().connect(sigc::mem_fun(*this,&MyClass::cellRendererValidatedOnEdited)); 

I have to append column
treeView->append_column("",treeViewPointer->treeColumn.col_name); and treeView->append_column(treeviewColumnValidated);

Which is creating two columns totally wrong.

My objective I want to be able to detect

  1. When the user starts editing the row (parent or child), which I can catch from signal_editing_started
  2. When the user completes the editing of the row, which I can catch from signal_edited

How do I create a CellRendererText to all the rows including parent & child.

Can anyone guide me?

I found this In Gtk, is it possible to have one cell renderer per treeview row?
I think this is exactly what I want but have no idea how to implement

I found this 8.2.5.2. Implementing custom logic for editable cells

The way suggested there is

For instance, maybe you want to restrict the input to certain characters or ranges of values.

To achieve this, you should use the normal
Gtk::TreeView insert_column() and
append_column() methods,
then use get_column_cell_renderer()
to get the Gtk::CellRenderer used by that column.

You should then cast that Gtk::CellRenderer* to the specific CellRenderer that you expect, so you
can use specific API.

For instance,
for a CellRendererText, you would set the cell’s editable property to true, like so:
cell.property_editable() = true;

You can then connect to the appropriate “edited” signal.
For instance, connect to
Gtk::CellRendererText::signal_edited(), or
Gtk::CellRendererToggle::signal_toggled()
If the column contains more than one CellRenderer then you will need to use Gtk::TreeView::get_column() and
then call get_cell_renderers() on that view Column.
In your signal handler, you should examine the new value and then store it in the Model if that is
appropriate for your application.

Can anyone guide me here as to how to implement this please

Got a solution, which worked for me.

Hope it helps few others too.

I came across this 8.2.5.2. Implementing custom logic for editable cells. to come up with this logic

Gtk::CellRendererText *cellT = (Gtk::CellRendererText*)(treeView->get_column_cell_renderer(0));
cellT->property_editable() = true;
cellT->signal_edited().connect(sigc::mem_fun(*this,&MyClass::cellRendererEdited));

Gtk::CellRendererText

I was able to get void on_my_edited(const Glib::ustring& path, const Glib::ustring& new_text)

path to know which treeview row was selected and new_text the updated new text entered by the user after he presses ENTER.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.