1

I am writing an pygtk app that uses a treeview, which has a connection to button_press_event. What I can't figure out is how to pass the information about the treeview (specifically which row is clicked) to a gtk.Menu or another method. If I used the row-activated signal, it passes in the row and column information in as an argument, but this does not happen with button_press_event. Here is the code in question:

    self.liststore = gtk.ListStore(str,int, int, int,str, 'gboolean')
    self.treeview = gtk.TreeView(self.liststore)

    self.treeview.connect("button_press_event",self.serverListEvent)
    self.treeview.set_search_column(0)
    self.draw_columns(self.treeview)

    self.blackmenu = gtk.Menu()
    self.bitem = gtk.MenuItem("Blacklist server")
    self.blackmenu.append(self.bitem)
    self.bitem.connect("activate",self.blacklistServer)
    self.bitem.show()

def serverListEvent(self,treeview,event):
    x = int(event.x)
    y = int(event.y)
    time = event.time
    model = treeview.get_model()

    pthinfo = treeview.get_path_at_pos(x, y)
    if pthinfo is not None:
        path, col, cellx, celly = pthinfo
        # Error here for the model with the column
        print 'url clicked '+model[col][0]
        treeview.grab_focus()
        treeview.set_cursor( path, col, 0)
        # Popup blacklist menu on right click
        if event.button == 3:            
            self.blackmenu.popup( None, None, None, event.button, time)
        # Join game on double click
        elif event.type == gtk.gdk._2BUTTON_PRESS:
            self.joinGame(treeview,model[col][0])
    return True

I then need to pass the information from the clicked row to the self.joinGame and self.blacklistServer methods, but don't know how to do this either.

gpoo
  • 8,408
  • 3
  • 38
  • 53
Jonno_FTW
  • 8,601
  • 7
  • 58
  • 90

1 Answers1

2

I remember that one being kindy tricky. I set up my treeview's gtk.TreeSelection to whatever mode of selection preferred. Then this:

def get_selected_ids(self):
    ids = []
    store, paths = self.get_selection().get_selected_rows()

    # self.adapter is a mapping class from my data-model to a model column index.
    # If you know the column index from your gtk.ListStore, you don't need it.
    colindex = self.adapter.get_column_modelindex("id")

    for path in paths:
        treeiter = store.get_iter(path)
        # Pull value from liststore
        val = store.get_value(
            treeiter, 
            colindex
        )
        ids.append(val)
    return ids

Edit:

I never used gtk.Menu and gtk.MenuItem, so I didn't get that you already had your menu connected to a callback (blacklistServer). This is untested, but should give you an idea.

def blacklistServer(self, menuitem, *ignore):
    # you have to modify *get_selected_ids* so it returns the values you need for
    # blacklisting.
    values = self.get_selected_ids()
    blacklist = set(values)
    self.saveBlacklist(blacklist)
XORcist
  • 4,288
  • 24
  • 32
  • How do I pass this information about what row is selected to the popup menu? – Jonno_FTW Apr 17 '11 at 08:15
  • 2 ways I can think of: you could register a custom signal with the treeview and have your popup listen to that. Or if you keep a reference to your popup you can set the value directly or with a method on it. – XORcist Apr 17 '11 at 08:27
  • Can you give some example code because I am really stuck here. – Jonno_FTW Apr 17 '11 at 08:55
  • Please see my edit. Are you asking about the *joinGame* method as well? – XORcist Apr 17 '11 at 09:37