学习Gtkmm,无法显示锚定在textview中的按钮

时间:2013-03-17 16:25:14

标签: gtk textview anchor gtkmm

我正在学习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();
}

这样我就可以获得一个固定在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);
}

1 个答案:

答案 0 :(得分:0)

必须按如下方式创建按钮:

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

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

m_TextView.add_child_at_anchor(*m_pbutton, refAnchor);

如果其他人需要帮助,请回答这个问题。