2013-03-17 20 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); 

回答了這個問題,以防其他人需要幫助。