Anzeige:
Results 1 to 4 of 4

Thread: GTK Tree-Model-View

  1. #1
    Registrierter Benutzer
    Join Date
    02.08.2008
    Posts
    177

    GTK Tree-Model-View

    Hi,

    ich versuche gerade Daten eines Struct Array in einem GTK Tree anzeigen zu lassen:
    Code:
    typedef struct{
          int value_a;
          int value_b;
          int value_c;
    } Daten[20];
    Dazu habe ich unter anderen die Anleitung aus der GTK Dokumentation anwenden wollen: https://developer.gnome.org/gtk3/stable/TreeWidget.html.
    Doch ich verstehe nicht, wie ich Daten in den Tree bekomme. Ich habe auch eine Anleitung mit Render gefunden:
    Code:
      renderer = gtk_cell_renderer_text_new();
    
      /* pack cell renderer into tree view column */
      gtk_tree_view_column_pack_start(col, renderer, TRUE);
    
      /* set 'text' property of the cell renderer */
      g_object_set(renderer, "text", "Boooo!", NULL);
    Doch schreibt dabei render die gesammte Spalte mit dem Text Boooo! über.
    Deshalb wollte ich Euch fragen, ob Ihr mir erklären könnt, wie ich in GTK einen Baum mit Werten füllen kann.

    Vielen Dank!
    dml
    Last edited by dml; 20-09-2013 at 09:46.

  2. #2
    Administrator anda_skoa's Avatar
    Join Date
    17.11.2001
    Location
    Graz, Österreich
    Posts
    5.477
    Bei so einem Model/View Setup wird der Renderer nicht direkt benutzt, sondern vom View.

    Der View wiederum erhält die Daten von einem Model.
    Sieh dir den Abschnitt "Creating a model" in der von dir verlinkten Dokumentation an.

    Ciao,
    _
    Qt/KDE Entwickler
    Debian Benutzer

  3. #3
    Registrierter Benutzer
    Join Date
    02.08.2008
    Posts
    177
    Das habe ich auch schon versucht, indem ich bei dem Code aus der Datei die GtkTreeViewColumns herrausgenommen habe, um die Daten von Model anzeigen zu lassen. Doch ich erhalte leider nur ein leeres Widget.
    Code:
     #include <gtk/gtk.h>
    
    enum
    {
      COL_FIRST_NAME = 0,
      COL_LAST_NAME,
      NUM_COLS
    } ;
    
    static GtkTreeModel *
    create_and_fill_model (void)
    {
      GtkTreeStore  *treestore;
      GtkTreeIter    toplevel, child;
    
      treestore = gtk_tree_store_new(NUM_COLS, G_TYPE_STRING, G_TYPE_STRING);
    
      /* Append a top level row and leave it empty */
      gtk_tree_store_append(treestore, &toplevel, NULL);
    
      /* Append a second top level row, and fill it with some data */
      gtk_tree_store_append(treestore, &toplevel, NULL);
      gtk_tree_store_set(treestore, &toplevel,
                         COL_FIRST_NAME, "Joe",
                         COL_LAST_NAME, "Average",
                         -1);
    
      /* Append a child to the second top level row, and fill in some data */
      gtk_tree_store_append(treestore, &child, &toplevel);
      gtk_tree_store_set(treestore, &child,
                         COL_FIRST_NAME, "Jane",
                         COL_LAST_NAME, "Average",
                         -1);
    
      return GTK_TREE_MODEL(treestore);
    }
    
    static GtkWidget *
    create_view_and_model (void)
    {
      GtkTreeViewColumn   *col;
      GtkCellRenderer     *renderer;
      GtkWidget           *view;
      GtkTreeModel        *model;
    
      view = gtk_tree_view_new();
    
    // Empty
    
      model = create_and_fill_model();
    
      gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);
    
      g_object_unref(model); /* destroy model automatically with view */
    
      gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)),
                                  GTK_SELECTION_NONE);
    
      return view;
    }
    
    
    int
    main (int argc, char **argv)
    {
      GtkWidget *window;
      GtkWidget *view;
    
      gtk_init(&argc, &argv);
    
      window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
      g_signal_connect(window, "delete_event", gtk_main_quit, NULL); 
    
      view = create_view_and_model();
    
      gtk_container_add(GTK_CONTAINER(window), view);
    
      gtk_widget_show_all(window);
    
      gtk_main();
    
      return 0;
    }
    Aber dann wird wohl etwas mit meinem Code Fehlerhaft sein; ich werde nach dem Fehler suchen.

    Danke für die Richtung.
    Last edited by dml; 20-09-2013 at 11:14.

  4. #4
    Registrierter Benutzer
    Join Date
    02.08.2008
    Posts
    177
    So, meine erste GTK Model-View Ansicht ist fertig:
    Code:
    #include <gtk/gtk.h>
    
    enum
    {
       TITLE_COLUMN,
       AUTHOR_COLUMN,
       CHECKED_COLUMN,
       N_COLUMNS
    };
      
    int
    main (int argc, char **argv)
    {
    
    	GtkWidget *window;
    
    	gtk_init(&argc, &argv);
    
    	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    	g_signal_connect(window, "delete_event", gtk_main_quit, NULL);
    
    	
    	GtkTreeStore *store = gtk_tree_store_new (N_COLUMNS,       /* Total number of columns */
    						  G_TYPE_STRING,   /* Book title              */
    						  G_TYPE_STRING,   /* Author                  */
    						  G_TYPE_BOOLEAN); /* Is checked out?         */
    	
    	
    	GtkTreeIter iter1;  /* Parent iter */
    	GtkTreeIter iter2;  /* Child iter  */
    
    	gtk_tree_store_append (store, &iter1, NULL);  /* Acquire a top-level iterator */
    	gtk_tree_store_set (store, &iter1,
    			    TITLE_COLUMN, "The Art of Computer Programming",
    			    AUTHOR_COLUMN, "Donald E. Knuth",
    			    CHECKED_COLUMN, TRUE,
    			    -1);
    
    	gtk_tree_store_append (store, &iter2, &iter1);  /* Acquire a child iterator */
    	gtk_tree_store_set (store, &iter2,
    			    TITLE_COLUMN, "Volume 1: Fundamental Algorithms",
    			    -1);
    
    	gtk_tree_store_append (store, &iter2, &iter1);
    	gtk_tree_store_set (store, &iter2,
    			    TITLE_COLUMN, "Volume 2: Seminumerical Algorithms",
    			    -1);
    
    	gtk_tree_store_append (store, &iter2, &iter1);
    	gtk_tree_store_set (store, &iter2,
    			    TITLE_COLUMN, "Volume 3: Sorting and Searching",
    			    -1);
    	
    	////////////////////////////////////////////////////////////////////////////////////////////
    	
    	
    	GtkWidget *tree;
    
    	tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
    
    	g_object_unref(store); /* destroy model automatically with view */
    
    	gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(tree)),
    				    GTK_SELECTION_NONE);
    	
    	g_signal_connect(tree, "row-activated", G_CALLBACK(onRowActivated), NULL);
    	
    	////////////////////////////////////////////////////////////////////////////////////////////
    	
    	
    	GtkTreeViewColumn *column;
    	GtkCellRenderer *renderer;
    	
    	/* Create a cell render and arbitrarily make it red for demonstration
    	 * purposes */
    	renderer = gtk_cell_renderer_text_new ();
    	g_object_set (G_OBJECT (renderer),
    			"foreground", "red",
    			NULL);
    
    	/* Create a column, associating the "text" attribute of the
    	 * cell_renderer to the first column of the model */
    	column = gtk_tree_view_column_new_with_attributes ("Author", renderer,
    							   "text", AUTHOR_COLUMN,
    							    NULL);
    
    	/* Add the column to the view. */
    	gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
    	
    	/* Second column.. title of the book. */
    	renderer = gtk_cell_renderer_text_new ();
    	column = gtk_tree_view_column_new_with_attributes ("Title",
    							      renderer,
    							      "text", TITLE_COLUMN,
    							      NULL);
    	gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
    
    	/* Last column.. whether a book is checked out. */
    	renderer = gtk_cell_renderer_toggle_new ();
    	column = gtk_tree_view_column_new_with_attributes ("Checked out",
    							      renderer,
    							      "active", CHECKED_COLUMN,
    							      NULL);
    	gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
    	
    	
    	////////////////////////////////////////////////////////////////////////////////////////////
    	
    	
    	gtk_container_add(GTK_CONTAINER(window), tree);
    
    	gtk_widget_show_all(window);
    
    	gtk_main();
    
    	return 0;
    }
    }
    "Note that the model is never populated with data — that is left as an exercise for the reader" hatte mich durcheinander gebracht, Englisch ist halt nicht meine Muttersprache. Auch das GtkTreeViewColumn angewandt werden muss.
    Nur das Editieren der Daten macht noch ein paar Probleme.


    check kann initialisiert werden:
    Code:
    void check (GtkCellRendererToggle * cell, gchar * path, GtkListStore * model)
    g_signal_connect (cell, "toggled", G_CALLBACK (check), store);
    siehe: www.stackoverflow.com/ questions/...
    Last edited by dml; 25-09-2013 at 10:58.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •