PyGTK how to "load-on-demand" data to TreeView

1.5k views Asked by At

I'm using PyGTK TreeView (as list) for my data. Is there any way to load more data on demand by going throu the list.

For example:

I have database table where is 200 000 records. At application start- loads 200 records from table. when i scroll download more data to treeView.

2

There are 2 answers

0
serge_gubenko On BEST ANSWER

I believe you could do following:

  1. Add empty lines to your model, I believe you should be using GtkListStore

  2. For the treeview cell renderer define a data function.

  3. In the data function check if current model row is loaded and load it from SQL if it's not. I guess you can use cashing here and load more then one row at once.

Below is an example of how to set up the data function:

cell = gtk.CellRendererText()        
column = gtk.TreeViewColumn('column0', cell, text=0)   
column.set_cell_data_func(cell, self.load_data)
self.list.append_column(column)

....

def load_data(self, column, cell, model, iter):
    # load data for the given model row
    print 'load data ' + model.get_value(iter, 0)

hope this helps, regards

0
Lixas On

Here is how i managed to achieve load-on-demand for treeview (list) My Gtk.TreeView is a child of Gtk.ScrolledWindow (everything is built with glade, but that is not important)

I've set up my treeview rendering as Serge said:

cell = gtk.CellRendererText()
col = gtk.TreeViewColumn("Column header text")
col.pack_start(cell, True)
col.set_attributes(cell,text=0)
col.set_cell_data_func(cell, self._loadDataTest, treeView)
treeView.append_column(col)

Function to handle:

def _loadDataTest(self, column, cell, model, iter, widget):
    pageSize = widget.get_parent().get_vadjustment().get_page_size()+20
    adjGap = widget.get_parent().get_vadjustment().get_upper() - widget.get_parent().get_vadjustment().get_value()
    if widget.is_focus() and model.get_path(iter)[0] >= len(model)-2 and adjGap < pageSize:
        for i  in range(0, 20): #simulate additional data
            model.append(["row%s"%len(model)-1])
        time.sleep(0.05) #to pause execution for a while, no need to add data fow times

Now new data is loaded when "pre-last" (second from bottom) row is visible and scrollBar is almost at bottom. In my case, sleep is used to pause while gtk.widgets are refreshed.

Works by keyboard navigation, by mouse drag of scrollBar or mouse clicking on scrollbar button scrollbar button Hope this will help someone