如何在 GTK 应用程序中创建多个视图
How to make multiple views in GTK application
我想制作一个具有多个视图的 GTK+ 应用程序,但我真的不知道如何以最佳方式实现它。在每个视图中,我都需要一些标签和按钮。首先,我尝试使用 GtkStack 制作应用程序,但 StackSwitcher 的自定义选项很差(它的按钮在行中,stackswitcher 图标太小,即使使用“icon-size” 属性 的最大可能尺寸也是如此)。
将堆栈的页面切换与普通按钮连接起来可以解决这个问题,但我不知道该怎么做。
我的第二种方法是使用多个 windows。我能够用按钮制作一些 windows 和 hiding/showing。不幸的是,该应用程序将在非常糟糕的电脑上运行(更多的电脑连接到触摸屏,这使得它的性能更差)并且在一些测试之后我可以说该应用程序有一些滞后。整个过程使所有 windows 都处于开始状态,然后只是隐藏或显示它们(取决于按下 window 的哪个按钮)。
总结一下我的问题:
- 制作此类应用程序的最佳方式是什么?使用多个 windows 还是使用 GtkStack?
- 如果使用 windows 如何优化整个事情?
- 如果使用堆栈如何在普通按钮上实现切换堆栈的选项卡?
我更喜欢 GtkStack。它有很棒的 gtk_stack_set_visible_child_name
,让您可以通过它的 ID 设置可见 child。在下面的代码片段中,我使用 GtkListBox 进行切换(我必须存储一个 GPtrArray
和 child 名称)
static void
row_activated (GtkListBox *box,
GtkListBoxRow *row,
gpointer udata)
{
MyWid *self = udata;
MyWidPrivate *priv = self->priv;
gint row_index = gtk_list_box_row_get_index (row);
gchar *path = g_ptr_array_index (priv->paths, row_index);
gtk_stack_set_visible_child_name (priv->stack, path);
}
如果你想使用GtkButton
事情就更简单了:
gchar *id; // just a string, that allows you to connect buttons and tabs
GtkWidget *child, *button;
child = create_tab_for_id (id); // not a real function! You should define it yourself
gtk_stack_add_named (stack, child, id);
button = create_button_for_id (id); // also not a real function
/* Time for magic */
g_signal_connect_swapped (button, "clicked",
G_CALLBACK (gtk_stack_set_visible_child_name),
stack);
我想制作一个具有多个视图的 GTK+ 应用程序,但我真的不知道如何以最佳方式实现它。在每个视图中,我都需要一些标签和按钮。首先,我尝试使用 GtkStack 制作应用程序,但 StackSwitcher 的自定义选项很差(它的按钮在行中,stackswitcher 图标太小,即使使用“icon-size” 属性 的最大可能尺寸也是如此)。
将堆栈的页面切换与普通按钮连接起来可以解决这个问题,但我不知道该怎么做。
我的第二种方法是使用多个 windows。我能够用按钮制作一些 windows 和 hiding/showing。不幸的是,该应用程序将在非常糟糕的电脑上运行(更多的电脑连接到触摸屏,这使得它的性能更差)并且在一些测试之后我可以说该应用程序有一些滞后。整个过程使所有 windows 都处于开始状态,然后只是隐藏或显示它们(取决于按下 window 的哪个按钮)。
总结一下我的问题:
- 制作此类应用程序的最佳方式是什么?使用多个 windows 还是使用 GtkStack?
- 如果使用 windows 如何优化整个事情?
- 如果使用堆栈如何在普通按钮上实现切换堆栈的选项卡?
我更喜欢 GtkStack。它有很棒的 gtk_stack_set_visible_child_name
,让您可以通过它的 ID 设置可见 child。在下面的代码片段中,我使用 GtkListBox 进行切换(我必须存储一个 GPtrArray
和 child 名称)
static void
row_activated (GtkListBox *box,
GtkListBoxRow *row,
gpointer udata)
{
MyWid *self = udata;
MyWidPrivate *priv = self->priv;
gint row_index = gtk_list_box_row_get_index (row);
gchar *path = g_ptr_array_index (priv->paths, row_index);
gtk_stack_set_visible_child_name (priv->stack, path);
}
如果你想使用GtkButton
事情就更简单了:
gchar *id; // just a string, that allows you to connect buttons and tabs
GtkWidget *child, *button;
child = create_tab_for_id (id); // not a real function! You should define it yourself
gtk_stack_add_named (stack, child, id);
button = create_button_for_id (id); // also not a real function
/* Time for magic */
g_signal_connect_swapped (button, "clicked",
G_CALLBACK (gtk_stack_set_visible_child_name),
stack);