我想在我的GtkTextView中有一个可滚动的GtkWindow,但我只想让GtkTextView部分而不是整个窗口滚动。我试着把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;
}

最佳答案

不确定为什么要使用固定容器。如果是,则必须提供包含textview的滚动窗口的宽度和高度。我已经编译了你的代码,它能工作。只需添加:

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

作为一个例子,这将在你的滚动窗口上设置200×200的大小。

关于c - 如何将GTKScrolledWindow放入另一个容器中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44224508/

10-13 09:43