2013-03-17 53 views
1

我学习gtkmm的锚定按钮,我发现在gnome.org一个TextView的例子:学习gtkmm的,不能显示在一个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."); 

} 

和construtor是:

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(); 
} 

这样我就可以获得锚定在textview中的按钮。 但是代码不起作用,按钮只是显示为矩形内的十字形状,点击时没有响应。

我还发现这些网站:

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); 
} 
+0

你想在文本视图下面放一排按钮,或者在文本中放一个按钮。此代码首先执行,或尝试执行。如果这是你正在尝试的,也许你可以添加结果的图片,但不清楚问题是什么。 – ergosys 2013-03-18 02:20:30

+0

我想要第二个。我编辑了这篇文章,也许我的问题现在已经很清楚了。感谢您的评论... – 549762085 2013-03-18 02:33:46

+0

我没有看到您将文本缓冲区与textview关联的位置。应该是某个地方的set_buffer。不知道你是如何得到任何东西。 – ergosys 2013-03-18 03:25:49

回答

0

按钮必须这样创建:

Gtk::Button* m_pbutton = Gtk::manage(new Gtk::Button("Text")); 

,然后你可以将它添加到锚:

m_TextView.add_child_at_anchor(*m_pbutton, refAnchor); 

回答了这个问题,以防其他人需要帮助。