如何在gtk上添加text_view滚动?

时间:2016-04-03 06:24:53

标签: c gtk gtk3 gnome

我已使用此功能>>

添加了txtview
 text_v = gtk_text_view_new()

bt这里滚动不起作用以及如何设置textview大小,使用过这一个=>>

gtk_widget_set_size_request(text_v,400,200);

bt也无效

1 个答案:

答案 0 :(得分:3)

这就是我这样做的方式:

#include<gtk/gtk.h>

int main(int argc, char* argv[])
{
    GtkWidget *textview, *window, *scrolledwindow;
    GtkTextBuffer *buffer;

    gtk_init(&argc, &argv); /*void gtk_init (int *argc,char ***argv);*/
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    gchar* sample_text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit,\n" \
        "sed do eiusmod tempor incididunt ut labore et dolore magna\n" \
        "aliqua. Ut enim ad minim veniam, quis nostrud exercitation\n" \
        "ullamco laboris nisi ut aliquip ex ea commodo consequat.\n"\
        "Duis aute irure dolor in reprehenderit in voluptate velit\n"\
        "esse cillum dolore eu fugiat nulla pariatur. Excepteur sint\n"\
        "occaecat cupidatat non proident, sunt in culpa qui officia\n"\
        "deserunt mollit anim id est laborum.";

    textview = gtk_text_view_new();
    gtk_widget_set_size_request(textview, 400, 200); // This is but a request. The sizes are not guaranteed.
    scrolledwindow = gtk_scrolled_window_new(NULL, NULL); 

    buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview));
    gtk_text_buffer_set_text(buffer, sample_text, -1);

    gtk_container_add(GTK_CONTAINER(scrolledwindow), textview);
    gtk_container_add(GTK_CONTAINER(window), scrolledwindow);
    gtk_widget_show_all(window);

    gtk_main();

    return 0;
}

如果您需要自定义滚动窗口,请查看here