Get data from selected row of gtk treeview - gtkmm, c++

耗尽温柔 提交于 2019-12-01 05:28:48

问题


I have a GTK application that has a window with a treeview 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 treeview.

This is the class for the columns:

class ModelColumns: 
public Gtk::TreeModel::ColumnRecord{
    public:

    ModelColumns(){ add(m_port_name); }

    Gtk::TreeModelColumn<Glib::ustring> m_port_name;
};

This is like in the example here but with only one column: http://www.lugod.org/presentations/gtkmm/treeview.html

This is the button click signal handler at the moment:

tvPorts is the treeview widget

tvPortsList is the listStore for the treeview

static
void on_btnPortSelectOK_clicked (){
    Glib::RefPtr<Gtk::TreeSelection> selection = tvPorts->get_selection();
    Gtk::TreeModel::iterator selectedRow = selection->get_selected();
    //Now what?
    //Need to get data from selected row to display it.
}

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.

As far as I can tell, I need to get a TreeRow object from my iterator (selectedRow) how do I do this?

Thanks.


Update:

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.

Glib::RefPtr<Gtk::TreeSelection> selection = tvPorts->get_selection();
Gtk::TreeModel::iterator selectedRow = selection->get_selected();
Gtk::TreeModel::Row row = *selectedRow;
Glib::ustring port = row.get_value(m_Columns.m_port_name);
printf("\nselected port: %s", port.data());

This seems odd. (m_Columns is an instance of the ModelColumns class)


Update 2:

Fixed the problem by adding fflush(stdout); It all works now, thanks.


回答1:


The docs say to simply dereference the iter to get the TreeRow:

Gtk::TreeModel::Row row = *iter;   // 'iter' being your 'selectedRow'
std::cout<<row[0]; 


来源:https://stackoverflow.com/questions/18448876/get-data-from-selected-row-of-gtk-treeview-gtkmm-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!