请问gtkmm中treeview如何添加事件。
请问gtkmm中treeview如何添加事件。我从数据库中读取数据:
while(没有读完)
{
.........
向treeview中添加数据。
想在这里给treeView添加事件 事件起因是 select_row。(gtk下是这个)
我就是想在单击某一行数据时 打出个新窗口,并把 这一行的数据 如: 1 张三 男 河北 传给打开的窗口。
.........
} 我从数据库中读取数据:
for(int i=0;i<rs_main.GetRecordCount();++i)
{
Gtk::TreeModel::Row row = *(refTreeModel_guest_room->append());
row = rs_main;
row = rs_main;
row = rs_main;
row = rs_main;
row = rs_main;
}
我的大概意思 就是 单击 第一行数据然后 弹出个 新窗口 ,在新窗口要用到此行的数据
http://gtkmm.org/docs/gtkmm-2.4/docs/tutorial/figures/treeview_list.png
//基本上按照gtkmm官方给的例子做的
File: examplewindow.h
#ifndef GTKMM_EXAMPLEWINDOW_H
#define GTKMM_EXAMPLEWINDOW_H
#include <gtkmm.h>
class ExampleWindow : public Gtk::Window
{
public:
ExampleWindow();
virtual ~ExampleWindow();
protected:
//Signal handlers:
virtual void on_button_quit();
//Tree model columns:
class ModelColumns : public Gtk::TreeModel::ColumnRecord
{
public:
ModelColumns()
{ add(m_col_id); add(m_col_name); add(m_col_number); add(m_col_percentage);}
Gtk::TreeModelColumn<unsigned int> m_col_id;
Gtk::TreeModelColumn<Glib::ustring> m_col_name;
Gtk::TreeModelColumn<short> m_col_number;
Gtk::TreeModelColumn<int> m_col_percentage;
};
ModelColumns m_Columns;
//Child widgets:
Gtk::VBox m_VBox;
Gtk::ScrolledWindow m_ScrolledWindow;
Gtk::TreeView m_TreeView;
Glib::RefPtr<Gtk::ListStore> m_refTreeModel;
Gtk::HButtonBox m_ButtonBox;
Gtk::Button m_Button_Quit;
};
#endif //GTKMM_EXAMPLEWINDOW_H
File: examplewindow.cc
#include <iostream>
#include "examplewindow.h"
ExampleWindow::ExampleWindow()
: m_Button_Quit("Quit")
{
set_title("Gtk::TreeView (ListStore) example");
set_border_width(5);
set_default_size(400, 200);
add(m_VBox);
//Add the TreeView, inside a ScrolledWindow, with the button underneath:
m_ScrolledWindow.add(m_TreeView);
//Only show the scrollbars when they are necessary:
m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
m_VBox.pack_start(m_ScrolledWindow);
m_VBox.pack_start(m_ButtonBox, Gtk::PACK_SHRINK);
m_ButtonBox.pack_start(m_Button_Quit, Gtk::PACK_SHRINK);
m_ButtonBox.set_border_width(5);
m_ButtonBox.set_layout(Gtk::BUTTONBOX_END);
m_Button_Quit.signal_clicked().connect( sigc::mem_fun(*this, &ExampleWindow::on_button_quit) );
//Create the Tree model:
m_refTreeModel = Gtk::ListStore::create(m_Columns);
m_TreeView.set_model(m_refTreeModel);
//Fill the TreeView's model
Gtk::TreeModel::Row row = *(m_refTreeModel->append());
row = 1;
row = "Billy Bob";
row = 10;
row = 15;
row = *(m_refTreeModel->append());
row = 2;
row = "Joey Jojo";
row = 20;
row = 40;
row = *(m_refTreeModel->append());
row = 3;
row = "Rob McRoberts";
row = 30;
row = 70;
//Add the TreeView's view columns:
m_TreeView.append_column("ID", m_Columns.m_col_id); //This number will be shown with the default numeric formatting.
m_TreeView.append_column("Name", m_Columns.m_col_name);
m_TreeView.append_column_numeric("Formatted number", m_Columns.m_col_number, "%010d" /* 10 digits, using leading zeroes. */);
//Display a progress bar instead of a decimal number:
Gtk::CellRendererProgress* cell = new Gtk::CellRendererProgress;
int cols_count = m_TreeView.append_column("Some percentage", *cell);
Gtk::TreeViewColumn* pColumn = m_TreeView.get_column(cols_count - 1);
if(pColumn)
{
#ifdef GLIBMM_PROPERTIES_ENABLED
pColumn->add_attribute(cell->property_value(), m_Columns.m_col_percentage);
#else
pColumn->add_attribute(*cell, "value", m_Columns.m_col_percentage);
#endif
}
//Make all the columns reorderable:
//This is not necessary, but it's nice to show the feature.
//You can use TreeView::set_column_drag_function() to more
//finely control column drag and drop.
for(guint i = 0; i < 2; i++)
{
Gtk::TreeView::Column* pColumn = m_TreeView.get_column(i);
pColumn->set_reorderable();
}
show_all_children();
}
ExampleWindow::~ExampleWindow()
{
}
void ExampleWindow::on_button_quit()
{
hide();
}
File: main.cc
#include <gtkmm/main.h>
#include "examplewindow.h"
int main(int argc, char *argv[])
{
Gtk::Main kit(argc, argv);
ExampleWindow window;
Gtk::Main::run(window); //Shows the window and returns when it is closed.
return 0;
} treeview->get_selection()->signal_changed().connect(sigc::mem_fun(*this,&Youclass:selectionChangedCallBack));
页:
[1]