gpt4 book ai didi

c - 使 GtkScrolledWindow 默认显示所有子项

转载 作者:太空宇宙 更新时间:2023-11-04 04:09:34 26 4
gpt4 key购买 nike

如何初始化 GtkScrolledWindow通过尽可能地增长来避免滚动条?

默认情况下它似乎越小越好。

演示代码(使用 SIGINT 退出):

#include <gtk/gtk.h>

int main(int argc, char *argv[]) {
GtkWidget* window;
GtkWidget* content;
GtkWidget* sw;

gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

sw = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_container_add(GTK_CONTAINER(window), sw);
gtk_widget_show(sw);

content = gtk_button_new_with_label("This is a very, very"
"very very very very very very very very long text");
gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), content);
gtk_widget_show(content);

gtk_widget_show(window);
gtk_main();

return 0;
}

最佳答案

我认为这既有效又不是丑陋的 hack(很多):

int main(int argc, char *argv[]) {
GtkWidget* window;
GtkWidget* content;
GtkWidget* sw;

GtkRequisition size;
GtkWidget* viewport;

gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

sw = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(sw),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_container_add(GTK_CONTAINER(window), sw);
gtk_widget_show(sw);

content = gtk_button_new_with_label("This is a very, very"
"very very very very very very very very long text");
gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(sw), content);
gtk_widget_show(content);

gtk_widget_show(window);

viewport = gtk_widget_get_ancestor(content, GTK_TYPE_VIEWPORT);
gtk_widget_size_request(viewport, &size);
gtk_widget_set_size_request(sw, size.width, size.height);

gtk_main();

return 0;
}

关于c - 使 GtkScrolledWindow 默认显示所有子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/444085/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com