GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Sep 02, 2014 4:17 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: [Solved] GtkTreeSelection issue
PostPosted: Mon Aug 13, 2012 8:36 pm 
Offline

Joined: Mon Aug 13, 2012 8:18 pm
Posts: 2
Hi! It's my first post here, I'll try to give all the information I think is useful so that anyone wanting to help me won't have it difficult! If I don't, please, ask for it, I may have been just not too smart to post that information.

The thing is I have a GtkWindow, inside it, there's a GtkVBox, and inside the box, a table full of buttons, and a GtkScrolledWindow with a GtkTreeViewInside. So I have a window with buttons and a GtkTreeView (of a GtkTreeStore).

I want to know in some of the callback functions of these buttons which row is selected (selection has been set to GTK_SELECTION_BROWSE, so one will almost always be selected). So, in my callback function I have:

Code:
orderViewGetter *getter=orderViewGetter::getInstance();
GtkTreeView *view=GTK_TREE_VIEW(getter->getView());

switch(id) //This is an enum, used to know which button was pressed
{
...
      case EDIT_ORDER:
         {//I put those brackets after compiler scolded me about scope
            string selectedName;
            GtkTreeModel *model;
            GtkTreeSelection *selection=gtk_tree_view_get_selection(view);
            GtkTreeIter iter;
            
            if(gtk_tree_selection_get_selected(selection, &model, &iter))
            {
               //Column's number is PETS, a value from an enum
               gtk_tree_model_get(model, &iter, PETS, &selectedName, -1);
               cout << selectedName;
            }
            else
            {
               cout << "No selection " << endl;
            }
         }
         break;
...
}



I get no errors of the compiler, nor runtime errors when I click the button, it just prints "No selection ". At first I hadn't put that "if" clause, and some errors appeared because that iterator wasn't valid.

I thought that the selected row might had been de-selected by clicking the buttons, so I disabled
them getting focus on click, but that didn't have any effect, just a more stylish click.

By the way, if that can help, I'm compiling with MinGW g++ and mingw32-make, in Window 7, using GTK 2.24. If you spot anything about style that might improve my coding, I will thank you for telling too :)

I hope you can help me, thanks in advance.


Last edited by Ryckes on Tue Aug 14, 2012 8:03 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: GtkTreeSelection issue
PostPosted: Tue Aug 14, 2012 7:08 am 
Offline
Familiar Face

Joined: Sat Jun 23, 2012 1:36 pm
Posts: 5
I can't see anything wrong before gtk_tree_selection_get_selected(). Test if view is valid, test if selection is valid. If selection is valid, test if the selection mode is actually what you think it is with gtk_tree_selection_get_mode(). A few strategically placed g_printf()'s will eventually nail down the source of your problem.


Top
 Profile  
 
 Post subject: Re: GtkTreeSelection issue
PostPosted: Tue Aug 14, 2012 8:02 am 
Offline

Joined: Mon Aug 13, 2012 8:18 pm
Posts: 2
Thank you for your answer ijontichy!

While writing an answer, and testing whether the selection mode was valid, I found that, in my Singleton-like class that I use for getting the TreeView pointer (I was suffering trying to get both the button id and the TreeView pointer in my callback function), I had initialised the TreeView with the model. All right. But then, while testing, I had put another gtk_tree_view_new_with_model(...) with the same pointer, so the Singleton's TreeView wasn't shown in the window. I did that because at first this Singleton didn't work and I was trying to spot the problem, and I forgot to remove this line.

That was the reason why in some other cases of the switch I could remove and insert rows, because they had the same model, but not quite the same TreeView. It works now, thank you very much!


Top
 Profile  
 
 Post subject: Re: GtkTreeSelection issue
PostPosted: Wed Aug 15, 2012 1:16 pm 
Offline
Familiar Face

Joined: Sat Jun 23, 2012 1:36 pm
Posts: 5
Ryckes wrote:
I was suffering trying to get both the button id and the TreeView pointer in my callback function...


Often when I need to access more than one piece of data in a callback function, I will use the following two functions: g_object_set_data() and g_object_get_data(). These allow you to attach data to your widget (or object). For example:

Code:
GtkWidget *my_edit;
GtkWidget *my_label;
gint age;
gchar *str;

...

g_object_set_data(G_OBJECT(my_edit), "my_label", my_label);
g_object_set_data(G_OBJECT(my_edit), "age", GINT_TO_POINTER(age));
g_object_set_data(G_OBJECT(my_edit), "surname", str);


And then when I want to access them:

Code:
GtkWidget *my_label = g_object_get_data(G_OBJECT(widget), "my_label");
gint age = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(widget), "age"));
gchar *str = g_object_get_data(G_OBJECT(widget), "surname");


Of course you need to manage the memory of the data yourself, unless you're merely casting (e.g. age), or if GTK looks after it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group