如何将 GTKScrolledWindow 放入另一个容器中?

How do I put a GTKScrolledWindow inside of another container?

我想在我的 GtkWindow 中有一个可滚动的 GtkTextView,但我只想滚动 GtkTextView 部分而不是整个 window。我试图将 GTKTextView 放入 GtkScrolledWindow 并将 GtkScrolledWindow 放入 GtkFixed 容器中,但是 GtkTextView 没有出现。但是,当我将 GtkTextView 直接放入 GtkFixed 容器时,它会显示出来。

#include <gtk/gtk.h>

GtkWidget *window, *scrolled_window, *fixed, *log_box, *button1;
GtkTextBuffer *log_box_buffer;

static void button1_clicked(GtkWidget *widget, gpointer data) {
    printf("button1_clicked\n");
    gtk_text_buffer_insert_at_cursor(log_box_buffer, "You clicked the button.\n", 24);
}

static void app_activate(GtkApplication *app, gpointer user_data) {

    window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(window), "Window Title Here");
    gtk_window_set_default_size(GTK_WINDOW(window), 700, 400);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

    scrolled_window = gtk_scrolled_window_new(NULL, NULL);

    fixed = gtk_fixed_new();

    button1 = gtk_button_new_with_label("Button 1");
    g_signal_connect(button1, "clicked", G_CALLBACK(button1_clicked), NULL);

    log_box_buffer = gtk_text_buffer_new(NULL);
    log_box = gtk_text_view_new_with_buffer(log_box_buffer);

    gtk_fixed_put(GTK_FIXED(fixed), button1, 50, 50);


    /* Here I tried to put the textview inside of the scrolled window and
       add the scrolled window to the fixed container. The textview
       doesn't show up when I do this. */

    gtk_container_add(GTK_CONTAINER(scrolled_window), log_box);
    gtk_fixed_put(GTK_FIXED(fixed), scrolled_window, 200, 50);


    /* I also tried putting the textview directly in the fixed container.
       This shows up, but obviously I can't scroll it. */

//  gtk_fixed_put(GTK_FIXED(fixed), log_box, 200, 50);


    gtk_container_add(GTK_CONTAINER(window), fixed);
    gtk_widget_show_all(window);
}

int main(int argc, char **argv) {

    GtkApplication *app;
    int status;

    app = gtk_application_new("the.application.id", G_APPLICATION_FLAGS_NONE);
    g_signal_connect(app, "activate", G_CALLBACK(app_activate), NULL);

    status = g_application_run(G_APPLICATION(app), argc, argv);

    g_object_unref(app);
    return status;
}

不确定为什么要使用固定容器。如果是这样,您必须提供包含文本视图的滚动 window 的宽度和高度。我已经编译了你的代码并且它有效。只需添加:

gtk_widget_set_size_request (GTK_WIDGET(scrolled_window), 200, 200);

例如,这将在您的滚动 window 上设置 200 x 200 的大小。