gtk-listbox -contents shown in middle of horizontal line, listbox does not fit window size

一世执手 提交于 2019-12-06 16:27:23

Answer to 1 and 2 it's all about alignment and expand properties.

  1. You can set expand properties while adding to the container with gtk_box_pack_* set of functions, eg., gtk_box_pack_start.
  2. You can set alignment with gtk_widget_set_halign and gtk_widget_set_valign functions

Applying these to your code:

#include <gtk/gtk.h>
#include <glib.h>
#include <stdlib.h>

static void destroy(GtkWidget *widget, gpointer data)
{
  gtk_main_quit();
}


int main(int argc, char *argv[])
{
  gtk_init(&argc, &argv);

  GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
  g_signal_connect(window, "destroy", G_CALLBACK(destroy), NULL);

  GtkWidget *notebook = gtk_notebook_new();
  gtk_container_add(GTK_CONTAINER(window), notebook);


  int count;
  int i;
  gchar *text;

  for (count = 1; count <= 5; count++)
  {
      GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
      text = g_strdup_printf("Page %d", count);
      GtkWidget *label = gtk_label_new(text);
      GtkWidget *scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
      gtk_box_pack_start (GTK_BOX(vbox), scrolledwindow, TRUE, TRUE, 0);
      GtkWidget *listbox = gtk_list_box_new();
      for (i=1; i<100; i++)
      {
          gchar *name = g_strdup_printf("Label %i", i);
          GtkWidget *label = gtk_label_new(name);
          gtk_widget_set_halign (GTK_WIDGET(label), GTK_ALIGN_START);
          gtk_container_add(GTK_CONTAINER(listbox), label);

      }
      gtk_container_add(GTK_CONTAINER(scrolledwindow), listbox);
      gtk_notebook_append_page(GTK_NOTEBOOK(notebook), vbox, label);
  }

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}

Notice gtk_box_pack_start when adding the scrolled window to the container, it's set to fill and expand. Also check the label halign being set at GTK_ALIGN_START.

The result should be:

About 3) it's too specific and could not really understand your goal. You should also separate questions so that the answers are specific to each problem.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!