Re: Treeview Sorting
| From: | André Jansen | Date: | Mon, 13 Nov 2006 00:43:44 +0000 |
| Subject: | Re: Treeview Sorting | ||
| References: | 1 | Groups: | php.gtk.general |
| Request: | Send a blank email to php-gtk-general+get-7464@lists.php.net to get a copy of this message | ||
On 12 Nov 2006 at 17:37, André Jansen wrote:
> Hi all,
>
> I'm working on a database front end, which uses a GtkTreeView to display
> search results. The column headers are clickable so I can sort the
> results, using the default sorting functions of the GtkListStore. Works
> really neat. Except for one thing: If I happen to do a new search *while
> the result list is in a sorted state*, the whole app will freeze and peg
> the processor at 100% for as long as several minutes, before the search
> results are finally displayed and things turn back to normal. For the
> record: my development machine is a PIII 500...
>
> I figure that this behaviour is caused by the list model having to re-
> sort each time a new row is inserted. A typical query might return a few
> hundred rows.
>
> Therefore, the solution would probably be to reset the list to its
> default, unsorted state before a new query is run. How do I do this?
After having been pulling my hair out over this for a while, I did find a
solution that works.
I found that GtkTreeModelSort has a method 'reset_default_sort_func()'
which should do exactly what I wanted to do. In order to use it, I have
to 'overlay' my GtkListStore with a GtkTreeModelSort and use the latter
as the model for the GtkTreeView.
The following pseudocode describes the fix, which eliminated the problem
completely.
$treeview = new GtkTreeView();
...
// add columns, ecc.
...
$model = new MyListStore()
// MyListStore is a custom class which extends GtkListStore.
// It has amongst others a method for loading data directly from a
// database.
$sortedModel = new GtkTreeModelSort($model);
$treeview->set_model($sortedModel);
// $sortedModel mirrors the contents of $model, but can be sorted
// independently.
...
// Now, before I load a new dataset in $model, I must remove any sort
// that may have been applied to $sortedModel by the user clicking on
// a column header.
$sortedModel->reset_default_sort_func();
// $treeview however does not know about this, and the sort arrow will
// remain in the column header unless we 'forcibly' remove it:
for ($i = 0; $i < $treeview->get_n_columns(); $i++) {
$treeview->get_column($i)->set_sort_indicator(null);
}
// now we can load the new dataset:
$model->load($querystring);
// MyListStore::load() clears the model, then executes the query string
// passed as the argument, fetches the data and loads it into the model.
// I could probably improve performance by calling $model->clear() before
// calling $sortedModel->reset_default_sort_func()...
Comments welcome.
Cheers,
--
André
===========================================================
Black Holes were created when God divided by zero.