There is a table with data in the ListStore model. The data is displayed by the TreeView component. When you hover over a line, it becomes highlighted. How to get access to the fields of this selected line? I tried to do this with the following C code snippet:

GtkTreeSelection *psel; GtkTreeIter iter; GValue value; char *pstr; gint num; // psel=gtk_tree_view_get_selection( (GtkTreeView*)(pTreeView) ); //ничего не выбрано num=gtk_tree_selection_count_selected_rows(psel); if ( num<1 ) return; // gtk_tree_selection_get_selected (psel,(GtkTreeModel**)(&pModel)),&iter); // //здесь появляются ошибки gtk_tree_model_get_value((GtkTreeModel*)(pModel) ,&iter,0,&value); pstr=(char*)g_value_get_string(&value); gtk_label_set_text(pLabel,pstr ); 

As I understand it, I'm doing something wrong. In theory, I should be able to access the selected fields through pTreeView, and not through the model. There may be no model at all. According to the documentation I can not understand anything. And I did not find anything on the Internet. But the task is trivial.

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

Found in the documentation https://www.opennet.ru/docs/RUS/gtk-reference/ answer to your question. The model has access to fields through an iterator. In my case it’s like this:

 GtkTreeSelection *psel; GtkTreeIter iter; GValue value; char *pstr; gint num; // psel=gtk_tree_view_get_selection( (GtkTreeView*)(pTreeView) ); //ничего не выбрано num=gtk_tree_selection_count_selected_rows(psel); if ( num<1 ) return; // gtk_tree_selection_get_selected (psel,(GtkTreeModel**)(&pModel)),&iter); char *psId,*psNm; gtk_tree_model_get((GtkTreeModel*)(Guide.pStore[0]),&iter,0,&psId,1,&psNm,-1); gtk_label_set_text(Guide.plbIdSrv,psId ); gtk_label_set_text(Guide.plbNameSrv,psNm); g_free(psId); g_free(psNm); 

    Of course, it is better to use the GTK + 3 documentation. The documentation is of course complete, but in English and some things are not obvious.

    For example information on GtkTree

     GtkTreeModel * tree_model; GtkListStore * list_store; GtkTreeModelFilter * tree_model_filter; GtkTreeModelSort * tree_model_sort; GtkTreeStore *tree_store; 

    It seems different structures and different functions but

     tree_model = (GtTreeModel*) list_store; tree_model = (GtTreeModel*) tree_model_filter; tree_model = (GtTreeModel*) tree_model_sort; tree_model = (GtTreeModel*) tree_store; 

    Fully legal action.

    And in all functions

     gtk_tree_model_*(); gtk_tree_model_filter_*(); gtk_list_store_*(); gtk_tree_store_*(); gtk_tree_model_sort_*(); 

    You can use one pointer.

    At the same time, GtkTreeView can not be redrawn explicitly, even a small change in the structure can redraw the entire tree.