How to update Gtk::TreeModel::Row from external function
-
13-09-2019 - |
Question
Currently I'm developing a multi-thread application. I use a TreeView to display the states of each thread, one row per thread. There are mainly two classes:
- Main GUI class containing TreeView
- class for thread handling
Passing Gtk::TreeModel::iterator as an argument to the second class is not viable since we cannot access the elements in row in formats like row[m_Columns.m_id]. Using Glib::Dispatcher is also unavailable since the elements we change in the external function is thread-specific.
So, is there any practical method to update GUI from external functions?
Solution
It's available to declare a class for Columns in an external file and include the file in both GUI class file and thread class file.
Like
class Columns : public Gtk::TreeModel::ColumnRecord
{
public:
Gtk::TreeModelColumn<unsigned int> m_id;
Gtk::TreeModelColumn<Glib::ustring> m_pin;
Gtk::TreeModelColumn<Glib::ustring> m_name;
Gtk::TreeModelColumn<unsigned int> m_percentage;
Gtk::TreeModelColumn<Glib::ustring> m_status;
Columns()
{
add(m_id);
add(m_pin);
add(m_name);
add(m_percentage);
add(m_status);
}
};
So that if you created a Columns instance m_columns in GUI class, and passed it as a parameter to thread class, you can use
(*row)[m_columns.m_id]
to access the elements in TreeModel.
OTHER TIPS
I think you should rethink your architecture. The simpliest and safest way is for your thread to send information in a thread safe way to a class that will gather them. Then make your GUI thread read those informations, change your treevien and then refresh.
I use this paradigm in a big gtkmm/multithreaded application. Remember that it is always better to centralize your synchronization code.
I hope it helps.