How to get total number of rows in Gtkmm TreeStore

I want to use remove all the rows from treeStore for which I can call clear()

But in the beginning when there is no rows to be delete, I need to be able to check what is the size of the TreeStore for which I came to know from this How to get the number of rows of gtk.ListStore we can use Gtk::TreeModel::iter_n_children_vfunc

But I don’t know how to use it, any help on how to use it?

When user clicks on any row I know we can to get the iter

Gtk::TreeModel::iterator iter = imgTPTreeViewPointer->get_iter(path);

But since nothing is clicked I don’t know how to create theiter here

Any help would be useful

You may wanna use get_iter_first() instead.

Gtk::TreeIter *iter;
bool tree_has_rows;

tree_has_rows = imgTPTreeViewPointer->get_iter_first(iter);

The site that you link to suggests using gtk_tree_model_iter_n_children().
The corresponding function in gtkmm is Gtk::TreeNodeChildren::size().
You should be able to use imgTPTreeViewPointer->children().size().

Don’t use Gtk::TreeModel::iter_n_children_vfunc() unless you make your own
implementation of the TreeModel interface.

If you just want to remove all rows in your TreeStore, must you really know
if it’s empty or not? Can’t you call Gtk::TreeStore::clear() unconditionally?
If the TreeStore is empty, clear() should do nothing.

1 Like

No function called get_iter_first I get it as the error

imgTPTreeViewPointer->children().size() does give the size of the treeStore but clear() doesn’t remove the row from the GUI

imgTPTreeViewPointer->clear(); doesn’t seem to work. I can still see the previous row data on the GUI.

Tried to remove row using erase saw this Removing multiple rows from a Gtk TreeStore which mentions that the removal process has to be reversed.

    if (imgTPTreeViewPointer->children().size()>0)
    {
        auto children = imgTPTreeViewPointer->children();
        for (auto iter = children.end(),begin = children.begin();iter != begin;--iter)
        {
            auto row = *iter;
            imgTPTreeViewPointer->erase(iter);
        }
    }

I get his error

gtkmm:ERROR:treestore.cc:51:Gtk::TreeModel::iterator Gtk::TreeStore::erase(const iterator&): assertion failed: (iter.get_gobject_if_not_end() != nullptr)
Bail out! gtkmm:ERROR:treestore.cc:51:Gtk::TreeModel::iterator Gtk::TreeStore::erase(const iterator&): assertion failed: (iter.get_gobject_if_not_end() != nullptr)
Aborted (core dumped)

I have raised a separate issue for TreeView not getting erasing the previous rows after I do TreeStore->clear() here TreeStore clear not erasing the previous data. Please do help in case you know how to correct it

As in typical C++ containers, children.end() points one step behind the last element
in the container. TreeNodeChildren has no rbegin() and rend() iterators, which makes
it difficult to iterate in reverse.

I tested ListStore::clear() and TreeStore::clear() by modifying one of gtkmm’s
demo programs, demos/gtk-demo/example_treeview_editable_cells.cc · gtkmm-3-24 · GNOME / gtkmm · GitLab.
It works as expected. The removed rows are erased from the GUI.
I don’t know what makes your program different.

BTW, do you use gtkmm3 or gtkmm4? I tested with gtkmm3.

Gtkmm-3.24

I implemented using this Chapter 9. The TreeView widget

First example
Where first we created class

  //Tree model columns:
  class ModelColumns : public Gtk::TreeModel::ColumnRecord
  {
  public:

    ModelColumns()
    { add(m_col_id); add(m_col_name); add(m_col_number); add(m_col_percentage);}

    Gtk::TreeModelColumn<unsigned int> m_col_id;
    Gtk::TreeModelColumn<Glib::ustring> m_col_name;
    Gtk::TreeModelColumn<short> m_col_number;
    Gtk::TreeModelColumn<int> m_col_percentage;
  };

Create an object ModelColumns m_Columns;

Create TreeModel and set Model

  Gtk::TreeView m_TreeView;
  Glib::RefPtr<Gtk::ListStore> m_refTreeModel;

//Create the Tree model:
  m_refTreeModel = Gtk::ListStore::create(m_Columns);
  m_TreeView.set_model(m_refTreeModel);

So TreeModel still contains previous data which I don’t know how to delete

I added a Clear button with a signal handler in the treeview/list example

void ExampleWindow::on_button_clear()
{
  std::cout << "Before clear(): " << m_refTreeModel->children().size() << std::endl;
  m_refTreeModel->clear();
  std::cout << "After clear(): " << m_refTreeModel->children().size() << std::endl;
}

When the Clear button is pressed, the result is as expected

Before clear(): 3
After clear(): 0

and the 3 rows are deleted from the GUI.

Have I understood you correctly? When you call m_refTreeModel->clear(), nothing happens.
The ListStore still contains 3 rows and those 3 rows are shown in the GUI.

For me also it shows number of children are 0 after clear() but I can see the rows on the GUI

Reference Pointer
Glib::RefPtr<Gtk::TreeStore> imgTPTreeViewPointer;

Class for TP

    class imgTPTreeModelCol: public Gtk::TreeModel::ColumnRecord
    {
    public:
        Gtk::TreeModelColumn<Glib::ustring> col_name;

        imgTPTreeModelCol()
        {    add(col_name);}

        virtual ~imgTPTreeModelCol()
        {}

    };
    imgTPTreeModelCol* imgTPTreeColumn = new imgTPTreeModelCol;

During construction of the object

    imgTPTreeViewPointer = Gtk::TreeStore::create(*imgTPTreeColumn);
    builder->get_widget("imgTPTreeView"+concatString,imgTPTreeView);
    imgTPTreeView->set_model(imgTPTreeViewPointer);
    imgTPTreeView->set_headers_visible(false);


    imgTPTreeView->set_activate_on_single_click(true);
    imgTPTreeView->append_column("",imgTPTreeColumn->col_name);
    imgTPTreeBox->set_size_request(60,145);
    imgTPTreeView->signal_row_activated().connect(sigc::mem_fun(*this,&Grid::selectedTPOption));

Clear the Reference Pointer through a function call

void Grid::clearTPData()
{
    imgTPTreeViewPointer->clear();
}

The GUI rows remains, when I add new rows it gets appended to the previous row.

If previous data had 3 rows after I clear children shows as 0, then I add additional 3 rows. The new data gets appended are new rows. Top three rows contain latest data, while the last three contain the old data.

imgTPTreeColumn still has the previous values those are being shown I guess. Don’t know how to clear it as the class has only add no clear or remove according to this Gtk::TreeModelColumnRecord Member List

I can’t explain why your GUI is not updated correctly. I found, though, that issues
have been reported concerning a pixel cache in GtkTreeView. See for instance gtk#503.
Different versions of gtk may behave differently. I’ve tested with gtk 3.24.36.
Which version of gtk have you got? Note that it’s the gtk (a.k.a gtk+) version
that matters, not the gtkmm version.

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