GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Jul 24, 2014 5:44 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Retrieving column data from a TreeView
PostPosted: Mon Dec 03, 2007 4:23 am 
Offline
Familiar Face

Joined: Sun Nov 05, 2006 8:56 am
Posts: 19
I've made an editable TreeView Component with a ListStore Model. I need to be able to tell when a cell is edited (via signal_edited), what the name of the column is thats throwing the signal and data of other columns in the same row. My Program recieves its data from MySQL and will be modifying the data in the database as well.

I'm using Gtkmm 2.4 on Windows XP using MSVC++ 2005 express edition. A code example in either Gtk+ or Gtkmm will work, but Gtkmm is prefered.


Setting up the signals for the columns:
Code:
  // set column properties
  for (unsigned int x=0; x<m_TreeView.get_columns().size(); x++)
  {
    m_TreeView.get_column(x)->set_reorderable();
    m_TreeView.get_column(x)->set_clickable();
    m_TreeView.get_column(x)->set_resizable();
    m_TreeView.get_column(x)->set_sort_column(x);

    // Get the column and connect a signal to it
    Gtk::CellRendererText * rendererText =
      dynamic_cast<Gtk::CellRendererText *>(m_TreeView.get_column_cell_renderer(x));

    rendererText->signal_edited().connect(sigc::mem_fun(*this, &CMainWindow::OnCellEdited));
  }



My failed attempts for OnCellEdited():
Code:
void CMainWindow::OnCellEdited(const Glib::ustring& path_string, const Glib::ustring& new_text)
{
  std::cout << "Cell Edited: Original: "<< path_string << " Edited: " << new_text << "\n";


  Gtk::TreeModel::iterator iter = m_TreeView.get_selection()->get_selected();
 
 
  /*
    {
      Gtk::TreeModel::Row row = *RowIter;

      std::vector<std::string *> ColumnValues;

      for(unsigned int i=1; i<m_TreeView.get_columns().size(); i++)
      {
        ColumnValues.push_back(
          new std::string(
            (*iter)[m_Columns.m_col_last_name]
            )
          );
      }
  */


  /* Gtk::TreeModel::iterator iter = m_TreeView.get_selection()->get_selected();
 
  if (iter)
  {
    Gtk::TreeModel::Row row = *iter;
    // Don't know how to grab the current column edited so just grabbing all
    // columns and altering whole row, should only add slight overhead to database
   
   

    ColumnValues.push_back(new std::string(row->get_value(m_Columns.m_col_last_name) ) );
    ColumnValues.push_back(new std::string(row->get_value(m_Columns.m_col_first_name) ) );
    ColumnValues.push_back(new std::string(row->get_value(m_Columns.m_col_middle_init) ) );
    ColumnValues.push_back(new std::string(row->get_value(m_Columns.m_col_gender) ) );
    ColumnValues.push_back(new std::string(row->get_value(m_Columns.m_col_phone) ) );
    ColumnValues.push_back(new std::string(row->get_value(m_Columns.m_col_email) ) );
    ColumnValues.push_back(new std::string(row->get_value(m_Columns.m_col_company) ) );
   
    std::cout << row->get_value(m_Columns.m_col_id) << " ";
    for(int i=0; i<ColumnValues.size(); i++)
      std::cout << ColumnValues.at(i) << " ";
    std::cout << std::endl;
    //database.EditRow(row->get_value(m_Columns.m_col_id), ColumnValues);
    */
  }


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 03, 2007 2:46 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
Each tree view column can have one or more cell renderers, but not the other way around. Your OnCellEdited() callback function is called when that one cell renderer is edited, so you should know what column it belongs to.

If you are connecting multiple renderers to this, try creating one callback for each renderer, and then calling a secondary function from the callback in order to avoid code replication.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 05, 2007 8:43 am 
Offline
Familiar Face

Joined: Sun Nov 05, 2006 8:56 am
Posts: 19
openldev wrote:
If you are connecting multiple renderers to this, try creating one callback for each renderer, and then calling a secondary function from the callback in order to avoid code replication.


How do I call a specific column? This obviously won't work:
Code:
Gtk::CellRendererText * rendererText = static_cast<Gtk::CellRendererText *>(
    m_TreeView.get_column_cell_renderer(m_Columns.m_col_last_name) );
  rendererText->signal_edited().connect(sigc::mem_fun(*this, &CMainWindow::OnLastNameCellEdited));


I really don't want to call get_column_cell_renderer(int n), I'd rather get it by a name maybe like above? if I do it like that I think i'd have to make enums saying which column is where, but if the columns are reorderable doesn't this throw that idea out the door?

Also once I get into the signal handler I want to be able to grab the data in another cell render text in the same row; I haven't figured out a way to do this.
Code:
// this works:
row[m_Columns.m_col_*] = value;

// I want something like this:
value = row[m_Columns.m_col_*];


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 05, 2007 12:29 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
Well, as for your second question, just make a structure that holds all of the objects that you are going to need, and send it to the callback function.

As for reordering columns. This is fine, but even if you do this, one or more cell renderers always belong to the same column regardless of its position. I think you should check out this tutorial on GtkTreeView: http://scentric.net/tutorial/

It is in C, but the important part is that you better understand the Model-View-Controller structure of the tree view widget.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group