title: Get data from selected row of gtk treeview - gtkmm, c++
body: <p>I have a GTK application that has a window with a <code>treeview</code> and a button. When the button is clicked I need to get the data from the first (and only) column of the selected row in the <code>treeview</code>.</p>
  
  <p>This is the class for the columns:</p>
  
  <pre><code>class ModelColumns: 
  public Gtk::TreeModel::ColumnRecord{
      public:
  
      ModelColumns(){ add(m_port_name); }
  
      Gtk::TreeModelColumn&lt;Glib::ustring&gt; m_port_name;
  };
  </code></pre>
  
  <p>This is like in the example here but with only one column: <a href="http://www.lugod.org/presentations/gtkmm/treeview.html" rel="noreferrer">http://www.lugod.org/presentations/gtkmm/treeview.html</a></p>
  
  <p>This is the button click signal handler at the moment:</p>
  
  <p><code>tvPorts</code> is the <code>treeview</code> widget</p>
  
  <p><code>tvPortsList</code> is the <code>listStore</code> for the <code>treeview</code></p>
  
  <pre><code>static
  void on_btnPortSelectOK_clicked (){
      Glib::RefPtr&lt;Gtk::TreeSelection&gt; selection = tvPorts-&gt;get_selection();
      Gtk::TreeModel::iterator selectedRow = selection-&gt;get_selected();
      //Now what?
      //Need to get data from selected row to display it.
  }
  </code></pre>
  
  <p>I have searched the documentation and many examples to try and find out what to do next but can't find any examples for gtkmm, I can only find examples for c or python implementations.</p>
  
  <p>As far as I can tell, I need to get a <code>TreeRow</code> object from my <code>iterator</code> (<code>selectedRow</code>) how do I do this?</p>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p><strong>Update:</strong></p>
  
  <p>I am now using this code and it almost works.
  The only problem is that it prints the previous selection.
  The first time I select something and then press the button it prints only a new line. The second time it prints what was selected the first time, the third prints the second, etc.</p>
  
  <pre><code>Glib::RefPtr&lt;Gtk::TreeSelection&gt; selection = tvPorts-&gt;get_selection();
  Gtk::TreeModel::iterator selectedRow = selection-&gt;get_selected();
  Gtk::TreeModel::Row row = *selectedRow;
  Glib::ustring port = row.get_value(m_Columns.m_port_name);
  printf("\nselected port: %s", port.data());
  </code></pre>
  
  <p>This seems odd.
  (<code>m_Columns</code> is an instance of the <code>ModelColumns</code> class)</p>
  
  <hr>
  
  <p><strong>Update 2:</strong></p>
  
  <p>Fixed the problem by adding <code>fflush(stdout);</code>
  It all works now, thanks.</p>