从gtk树视图的选定行获取数据 - gtkmm,c ++ [英] Get data from selected row of gtk treeview - gtkmm, c++
问题描述
我有一个GTK应用程序,它有一个窗口 treeview
和一个按钮。当点击按钮时,我需要从 treeview
中选定行的第一个(而且只有)列获取数据。
这是类的类:
class ModelColumns:
public Gtk :: TreeModel: :ColumnRecord {
public:
ModelColumns(){add(m_port_name); }
Gtk :: TreeModelColumn< Glib :: ustring> m_port_name;
}
这就像这里的例子,但只有一列: http://www.lugod.org/presentations/gtkmm/treeview.html
这是当前的按钮点击信号处理程序:
tvPorts
code> treeview 小部件
tvPortsList
是 listStore
for treeview
static
void on_btnPortSelectOK_clicked(){
Glib :: RefPtr< Gtk :: TreeSelection> selection = tvPorts-> get_selection();
Gtk :: TreeModel :: iterator selectedRow = selection-> get_selected();
//现在什么?
//需要从所选行获取数据才能显示它。
}
我搜索了文档和许多示例,尝试找出应该做什么下一步,但找不到任何gtkmm的例子,我只能找到c或python实现的例子。
据我所知,我需要得到一个 TreeRow
对象从我的迭代器
( selectedRow
)
更新:
我现在正在使用这个代码,它几乎可以工作。
唯一的问题是它打印上一个选择。
第一次选择某项,然后按按钮,它只打印一行。第二次打印第一次选择的内容,第三次打印第二次打印等。
Glib :: RefPtr Gtk :: TreeModel :: iterator selectedRow = selection-> get_selected();
Gtk :: TreeModel :: Row row = * selectedRow;
Glib :: ustring port = row.get_value(m_Columns.m_port_name);
printf(\\\
selected port:%s,port.data());
这似乎很奇怪。
( m_Columns
是 ModelColumns
类的一个实例)
更新2:
通过添加<$ c $
文档说要简单地取消引用iter以获取TreeRow:
Gtk :: TreeModel :: Row row = * iter; //'iter'as your'selectedRow'
std :: cout<< row [0];
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.
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];
这篇关于从gtk树视图的选定行获取数据 - gtkmm,c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!