我用GtkVBox和GtkViewPort创建了一个列表 我正在通过两个向上/向下GtkButton进行滚动。
GtkAdjustment* adjustment;
adjustment = gtk_viewport_get_vadjustment(GTK_VIEWPORT(viewport_ptr));
gtk_adjustment_set_value(adjustment, gtk_adjustment_get_value(adjustment)+(gdouble)SCROLL_SIZE);
gtk_widget_show_all(viewport_ptr);
但是当我将小部件添加到VBox时,它会在VBox的末尾添加,因为我正在使用gtk_box_pack_start
。所以我想将视口滚动到这个新添加的最后一个小部件,它位于列表的底部。
答案 0 :(得分:1)
您是否尝试将调整设置为最大值:
gtk_adjustment_set_value(adjustment, gtk_adjustment_get_upper(adjustment));
请注意,您可能需要延迟此操作,或者从适当的事件中触发它,因为上限会随着滚动的vbox的增长而改变。
我的建议是将其挂钩到GtkVBox的size-allocate信号中。
答案 1 :(得分:1)
以下是一个示例,显示了@unwind在he suggests将调整挂钩到size-allocate信号时的含义。
/***
This basic example program shows how to (automatically) move the
scrollbar of a GTK+ text view to the very bottom after text has been added.
Public domain code by N.L.M. de Jonge.
gcc `pkg-config gtk+-3.0 --cflags` -O2 -std=c99 -g -pedantic -Wall \
-Wextra -Wshadow -Wpointer-arith -Wcast-qual -Wstrict-prototypes \
-Wmissing-prototypes -D_REENTRANT example.c -o example `pkg-config \
gtk+-3.0 --libs`
***/
#include <gtk/gtk.h>
#include <stdlib.h>
GtkAdjustment *adj;
GtkWidget *scroll;
GtkTextBuffer *buffer;
GtkTextIter iter;
int iCounter;
void ScrollToEnd (GtkWidget *widget, GdkRectangle *allocation);
void Add (GtkButton *button);
/*****************************************************************************/
void ScrollToEnd (GtkWidget *widget, GdkRectangle *allocation)
/*****************************************************************************/
{
if (widget != NULL) { }
if (allocation != NULL) { }
adj = gtk_scrolled_window_get_vadjustment
(GTK_SCROLLED_WINDOW (scroll));
gtk_adjustment_set_value (adj, gtk_adjustment_get_upper (adj));
}
/*****************************************************************************/
void Add (GtkButton *button)
/*****************************************************************************/
{
char sText[10];
if (button != NULL) { }
iCounter++;
snprintf (sText, 10, "\n%i", iCounter);
gtk_text_buffer_get_end_iter (buffer, &iter);
gtk_text_buffer_insert_with_tags_by_name (buffer, &iter,
g_locale_to_utf8 (sText, -1, NULL, NULL, NULL), -1, "red_fg", NULL);
/*** We just added text, but we do NOT call ScrollToEnd() here. ***/
}
/*****************************************************************************/
int main (int argc, char *argv[])
/*****************************************************************************/
{
GtkWidget *window, *box, *grid, *text, *button;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect (window, "delete_event",
G_CALLBACK (exit), NULL);
gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
gtk_widget_realize (window);
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add (GTK_CONTAINER (window), box);
gtk_widget_show (box);
grid = gtk_grid_new();
gtk_widget_show (grid);
gtk_box_pack_start (GTK_BOX (box), grid, TRUE, TRUE, 0);
scroll = gtk_scrolled_window_new (NULL, NULL);
gtk_widget_set_size_request (scroll, 200, 200);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
/***/
/*** We call ScrollToEnd() if scroll receives the size-allocate signal. ***/
/***/
g_signal_connect (scroll, "size-allocate", G_CALLBACK (ScrollToEnd), NULL);
gtk_grid_attach (GTK_GRID (grid), scroll, 0, 0, 1, 1);
g_object_set (scroll, "vexpand", TRUE, NULL);
g_object_set (scroll, "margin", 10, NULL);
gtk_widget_show (scroll);
text = gtk_text_view_new();
gtk_container_add (GTK_CONTAINER (scroll), text);
gtk_widget_show (text);
buffer = gtk_text_buffer_new (NULL);
gtk_text_buffer_create_tag (buffer, "red_fg", "foreground", "red", NULL);
gtk_text_view_set_buffer (GTK_TEXT_VIEW (text), buffer);
button = gtk_button_new_with_label ("Add");
g_signal_connect (button, "clicked", G_CALLBACK (Add), NULL);
gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 1, 1);
g_object_set (button, "hexpand", TRUE, NULL);
gtk_widget_show (button);
gtk_widget_show (window);
iCounter = 0;
gtk_text_buffer_get_end_iter (buffer, &iter);
gtk_text_buffer_insert (buffer, &iter, g_locale_to_utf8
("Click \"Add\" to add numbers.", -1, NULL, NULL, NULL), -1);
gtk_main();
return 0;
}
/*****************************************************************************/
作为奖励,它显示了如何添加彩色文字。