Search code examples
c++textviewgtkmmgtk-textbuffer

C++ GTKmm - How do I get the content of my TextView (TextBuffer)


I am learning GTKmm (C++) on Linux.

I want to get the content of a TextView via the press of a button.

The code std::cout << m_textBuffer->get_text(); does not output anything to the console, and I can't find enough explanation on the official website.

It seems that I need to use iterator, but trying to do so with m_textBuffer->start() in the function get_text() tells me that start() isn't a function of TextBuffer, as well as an end() equivalent.

The console output a few warning which might be related :

Gtk-CRITICAL **: gtk_text_buffer_get_iter_at_mark: assertion 'GTK_IS_TEXT_MARK (mark)' failed Gtk-CRITICAL **: gtk_text_layout_get_line_yrange: assertion '_gtk_text_iter_get_btree (iter) == _gtk_text_buffer_get_btree (layout->buffer)' failed

In the GUI constructor, I have this :

m_txtQuestion.set_buffer(m_textBuffer);

m_textBuffer = Gtk::TextBuffer::create();
m_textBuffer->set_text("")

Solution

  • (for gtkmm-2.4 and gtkmm-3.0):
    Instead of trying to set a buffer, just use the one in the TextView:

    m_txtQuestion.get_buffer()->set_text("My text.");
    

    Then to get the text use:

    m_txtQuestion.get_buffer()->get_text();