gtk-listbox -contents shown in middle of horizonta

2019-06-07 22:12发布

问题:

I wrote a code for listbox in gtk problems 1. the contents of listbox are shown in middle of horizontal line. I want it to be left aligned. 2. the listbox does not fit the window size 3.the list box is within a notebook tab. I want when the item in the listbox is selected some processing to be done(some function called), the current tab display is hidden and new tab is displayed with the results and a back button. When Back button is pressed it does 2 cases for 2 tabs in 1 tab- it just shows the previous content in tab 2- it does processing again and shows results in list box in original format.

my code is as follows

#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_container_add(GTK_CONTAINER(vbox), scrolledwindow);
      //GtkWidget *textview = gtk_text_view_new();
      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_container_add(GTK_CONTAINER(listbox), label);

 }
      gtk_container_add(GTK_CONTAINER(scrolledwindow), listbox);
      gtk_container_add(GTK_CONTAINER(vbox), scrolledwindow);

      gtk_notebook_append_page(GTK_NOTEBOOK(notebook), vbox, label);
    }

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}

回答1:

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.