我正在学习 gtkmm,我在 gnome.org 中找到了一个 textview 示例:
https://developer.gnome.org/gtkmm-tutorial/2.22/sec-textview-examples.html.en
我更改了 fill_buffers() 代码以将按钮添加到 textview 中:
void ExampleWindow::fill_buffers()
{
m_refTextBuffer1 = Gtk::TextBuffer::create();
m_refTextBuffer1->set_text("This is the text from TextBuffer #1.");
//learn
Gtk::TextIter iter = m_refTextBuffer1->get_iter_at_offset(5);
refAnchor = m_refTextBuffer1->create_child_anchor(iter);
m_Button_Text.signal_clicked().connect(sigc::mem_fun(*this,
&ExampleWindow::on_button_quit) );
m_TextView.add_child_at_anchor(m_Button_Text, refAnchor);
//m_Button_Text.show();
m_refTextBuffer2 = Gtk::TextBuffer::create();
m_refTextBuffer2->set_text(
"This is some alternative text, from TextBuffer #2.");
}
构造函数是:
ExampleWindow::ExampleWindow()
: m_Button_Quit(Gtk::Stock::QUIT),
m_Button_Buffer1("Use buffer 1"),
m_Button_Buffer2("Use buffer 2"),
m_Button_Text("Text Button")
{
set_title("Gtk::TextView example");
set_border_width(5);
set_default_size(400, 200);
fill_buffers();
m_ButtonBox.pack_start(m_Button_Buffer1, Gtk::PACK_SHRINK);
m_ButtonBox.pack_start(m_Button_Buffer2, Gtk::PACK_SHRINK);
m_ButtonBox.pack_start(m_Button_Quit, Gtk::PACK_SHRINK);
m_ButtonBox.set_border_width(5);
m_ButtonBox.set_spacing(5);
m_ButtonBox.set_layout(Gtk::BUTTONBOX_END);
//Connect signals:
m_Button_Quit.signal_clicked().connect(sigc::mem_fun(*this,
&ExampleWindow::on_button_quit) );
m_Button_Buffer1.signal_clicked().connect(sigc::mem_fun(*this,
&ExampleWindow::on_button_buffer1) );
m_Button_Buffer2.signal_clicked().connect(sigc::mem_fun(*this,
&ExampleWindow::on_button_buffer2) );
//Add the TreeView, inside a ScrolledWindow, with the button underneath:
m_ScrolledWindow.add(m_TextView);
//Only show the scrollbars when they are necessary:
m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
m_VBox.pack_start(m_ScrolledWindow);
//Add buttons:
m_VBox.pack_start(m_ButtonBox, Gtk::PACK_SHRINK);
add(m_VBox);
on_button_buffer1();
show_all_children();
}
这样我就可以获得一个锚定在文本视图中的按钮。但是代码不起作用,按钮只是在矩形内显示为十字形,单击时没有响应。
我还发现了这些网站:
http://ruby-gnome2.sourceforge.jp/hiki.cgi?tut-gtk2-txtw-icw
http://www.matrix44.net/blog/?p=1033
我比较了我的和上述网站之间的代码。我仍然无法弄清楚为什么我的代码不起作用......
非常感谢提前。我被这个问题困扰了一天...
++++++++++++++++++++++
其他代码可能有用:
void ExampleWindow::on_button_quit()
{
hide();
}
void ExampleWindow::on_button_buffer1()
{
m_TextView.set_buffer(m_refTextBuffer1);
}
void ExampleWindow::on_button_buffer2()
{
m_TextView.set_buffer(m_refTextBuffer2);
}