简体   繁体   中英

Can't get GtkTextBuffer from Text View - C, GTK3

I'm working on an assigment using GTK3+, Anjuta(the Glade plugin) and C.

I have a text view, but I'm having trouble getting the text buffer it's using.

The code that's giving me trouble is this:

    GtkWidget * text_view_hilera_1;
    GtkWidget * buffer;
    text_view_hilera_1 = gtk_builder_get_object(build_princ,"txt_hilera1");
    buffer = gtk_text_view_get_buffer(text_view_hilera_1);
    error(gtk_buildable_get_name(buffer));

Error() is a function that just outputs a string, for debugging. It seems that the get_buffer() function is not returning null, I've checked that. It's indeed returning something, a not-null pointer, but to what, I don't know, because buffer fails every assertion.

(nw:6368): Gtk-CRITICAL **: gtk_buildable_get_name: assertion `GTK_IS_BUILDABLE (buildable)' failed

It also fails GTK_IS_TEXT_BUFFER and GTK_IS_WIDGET. The text view is working alright, and printing its name with gtk_buildable_get_name.

I can share the XML for the UI, but it's too large for the post. If you want any specific sections, just ask me. Thanks for the help!

This is the XML for the text view:

                            <object class="GtkTextView" id="txt_hilera1">
                            <property name="width_request">19</property>
                            <property name="height_request">60</property>
                            <property name="visible">True</property>
                            <property name="can_focus">True</property>
                            <property name="vscroll_policy">natural</property>
                            <property name="wrap_mode">char</property>
                            </object>

The prototype for gtk_text_view_get_buffer() is:

GtkTextBuffer *gtk_text_view_get_buffer (GtkTextView *text_view);


Instead of declaring buffer as a GtkWidget * , declare it as GtkTextBuffer * .

Since you're using the generic GtkWidget * for text_view_hilera_1 , cast it to GtkTextView* with the GTK_TEXT_VIEW() macro when calling gtk_text_view_get_buffer() .

The important parts:

GtkWidget *text_view_hilera_1;
GtkTextBuffer *buffer;

//...builder-related code

buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(text_view_hilera_1) );

RE:

Your code is correct except *buffer should not be a GtkWidget but a GtkTextBuffer . Change your line
GtkWidget *buffer; to
GtkTextBuffer *buffer;
and no errors will appear.
To avoid macros declare textview a GtkTextView *

See for more:

  1. https://developer.gnome.org/gtk3/stable/GtkTextView.html

  2. https://developer.gnome.org/gtk3/stable/GtkTextBuffer.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM