treeview - how to get selection from a gtk3 combobox with tree model -


i'm new gtk. last gui application wrote used text mode gui in turbo c, have little catching do.

i'm using gtk write test harness code in embedded system. i'm using combobox tree model provide 2-level selection. got combobox display wanted, although don't have understaning of cell_renderer parts copied , pasted stack overflow question.

gtktreestore* model = gtk_tree_store_new(1,g_type_string) (initilise model hold desired strings using     gtk_tree_store_append , gtk_tree_store_set)  gtkwidget* combobox = gtk_combo_box_new_with_model(model); gtk_combo_box_set_entry_text_column(combobox, 0);  gtkcellrenderer *column = gtk_cell_renderer_text_new(); gtk_cell_layout_pack_start(gtk_cell_layout(combobox),column,true); gtk_cell_layout_set_attributes(gtk_cell_layout(combobox), column,"text", 0,null); 

this code worked display combobox. needed selection combobox. tried getting index combobox using gtk_combo_box_get_active (). index returned didn't me. sub-tree items, showed position relative parent. so, tried pull out text of selected option. bit more searching found me line pull text combobox:

gchar * selection = gtk_entry_get_text(gtk_entry(gtk_bin_get_child(gtk_bin(mycombobox)))); 

however, calling gave me following error, , returned (null).

(test.exe:3040): glib-gobject-warning **: invalid cast `gtkcellview' `gtkentry' (test.exe:3040): gtk-critical **: gtk_entry_get_text: assertion `gtk_is_entry (entry)' failed 

so, bit more googling indicated need initialise combobox "entry", updated initialisation of combobox to:

combobox = gtk_combo_box_new_with_model_and_entry(model); 

and partial success!!. can pull text combobox, but displays selection text twice, on combobox drop-down. once selection made, displays single in box itself. so, if model text is:

opt10       opt11   opt20       opt21   

the tree displays each item twice (selecting first opt11)

[opt10 opt10] >  opt10  opt10                   [opt11  opt11]    opt20 opt20 >   

once make selection, (say opt11) combobox shows selected text correctly, , call gtk_entry_get_text(.....) returns text "opt11" expect.

so, i'm @ dead-end. want query combobox either index uniquely identifies item in tree, or text string. have text string method working, makes combobox options display twice.

help?

thanks,

this should work... compile code command in comments. 'full' version, using model , such... if want select name list, can use gtkcomboboxtext, easier use...

/*  * main.c  * copyright (c) 2015 john coppens <john@jcoppens.com>  *   * standalone_filechooser free software: can redistribute and/or modify  * under terms of gnu general public license published  * free software foundation, either version 3 of license, or  * (at option) later version.  *   * standalone_filechooser distributed in hope useful,  * without warranty; without implied warranty of  * merchantability or fitness particular purpose.  * see gnu general public license more details.  *   * should have received copy of gnu general public license along  * program.  if not, see <http://www.gnu.org/licenses/>.  *  *   gcc -o main `pkg-config --libs --cflags gtk+-3.0` main.c  */  #include <stdio.h> #include <gtk/gtk.h>  int on_destroy(gtkwidget *win, gpointer data) {   gtk_main_quit();   return false; }  void sel_changed(gtkcombobox *cbbox, gpointer data) {   gtkliststore *store;   gtktreeiter iter;   int item_nr, ok;   char *item;    ok = gtk_combo_box_get_active_iter(cbbox, &iter);   printf("%i\n", ok);   store = gtk_list_store(gtk_combo_box_get_model(cbbox));   gtk_tree_model_get(gtk_tree_model(store), &iter,     0, &item_nr,     1, &item,     -1);    printf("item: %s, nr: %d\n", item, item_nr);   g_free(item); }  int main(int argc, char *argv[]) {   gtkwidget *win, *cbbox;   gtkcellrenderer *col;   gtkliststore *store;   gtktreeiter iter;   int i;   char *items[] = {"thingie 1", "thingie 2", "thingie 3"};    gtk_init(&argc, &argv);    win = gtk_window_new(gtk_window_toplevel);   g_signal_connect(g_object(win), "destroy", g_callback(on_destroy), null);    store = gtk_list_store_new(2, g_type_int, g_type_string);   (i = 0; < sizeof(items)/sizeof(char *); i++) {     gtk_list_store_append(store, &iter);     gtk_list_store_set(store, &iter,         0, i,         1, items[i],         -1);   }    cbbox = gtk_combo_box_new_with_model(gtk_tree_model(store));   g_object_unref(store);   col = gtk_cell_renderer_text_new();   gtk_cell_layout_pack_start(gtk_cell_layout(cbbox), col, true);   gtk_cell_layout_set_attributes(gtk_cell_layout(cbbox), col,          "text", 1,         null);    gtk_combo_box_set_id_column(gtk_combo_box(cbbox), 1);   g_signal_connect(g_object(cbbox), "changed", g_callback(sel_changed), null);     gtk_container_add(gtk_container(win), cbbox);    gtk_widget_show_all(win);    gtk_main();    return 0; } 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -