views:

363

answers:

2

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:

  1. Main GUI class containing TreeView
  2. 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?

+1  A: 

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.

ryanli
It is not clear if it is your solution or a comment/precision. If it is include it in the question not in an answer. StackOverflow is not ment to be a forum ...
neuro
If it your solution, be clearer :)
neuro
I've added some sample code.
ryanli
+1 for the added explanations / sample code.
neuro
A: 

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.

neuro
Could you please explain in detail that, how to find the row for each thread?
ryanli