PDA

View Full Version : Removing "sort" from a QTreeView



3dch
10th August 2006, 18:06
Hi

I just would like to know whether this is the way to do it.

I've a QSortFilterProxyModel attached to a QTreeView. Clicking in one of the tree view header columns sorts the column accordingly. Now I want to get rid of the sorting because all data gets removed from the model and then it gets filled with new data again. I tried several approaches amongst others with a model->clear() but didn't succeed. But I was successfull with model->sort(-1) i.e. I sorted a non-existent column. Nice that it works but actually I found nothing in the Qt docs which gives me the proof that this is correct and will also work with future Qt releases (I'm using Qt 4.1.4).

The working piece of code looks like this:



treeView->header()->setSortIndicatorShown(false);
model->sort(-1);


Can anybody tell me whether this is good practice or if not how to do it better?

Thanks!
Ernst

lauranger
11th August 2006, 17:34
I don't answer exactly to your question because my views are not authoritative at all, but
from what's in the code and from that it make sens that sorting on an inexistant column should yield a null-sorting, I believe
the probability that your hack will still be right is quite comfortable.
My2c.

3dch
11th August 2006, 20:31
Thanks for your opinion. As long as it works I'm happy with it...
Just to mention: In contrast to sorting an invalid column in a model as discussed here a sort of an invalid column in a tree view will not work (there's an assertion of the column number in the Qt code):



treeView->sortByColumn (-1); // Fails


Ernst

gfunk
11th August 2006, 22:17
I'm not sure I understand what you want. When you say remove sorting, do you just want the items, as more data is added to the model, to just be appended to the bottom of the view, like a journal? (ie, no real sorting besides being in chronological time order)

3dch
12th August 2006, 01:38
Well. let me explain a bit clearer.

I have a QTreeView with several columns which has a QSortFilterProxyModel attached as its model with treeView->setModel(MySortFilterProxyModel). The source of the QSortFilterProxyModel is a QAbstractItemModel which is set with MySortFilterProxyModel->setSourceModel (MyAbstractItemModel).

This means that the data displayed in the tree view stems from the QAbstractItemModel (referred to as 'model' further on) but gets filtered/sorted by the QSortFilterProxyModel (referred to as 'proxyModel' further on).

Now my app starts with an empty model. Over the time the model gets filled with data which is shown in the tree view. And this is the important point: as long as the user didn't explicitly request sorting the data is shown in chronological order, i.e. in the order the data is inserted into the model (as you guessed). The user then can sort the tree view by clicking in the column headers. The tree view remembers which column is currently sorted in what sort order. When new data is added to the model it gets inserted in the tree view according to the current sort order.

Now there can be situations where all data gets removed from the model, thus the tree view doesn't display anything. New data should be shown in chronological order again independent of any sorting applied before. Therefore I hardly tried to figure out how I can have the tree view forget about any sorting order from before. The only solution to this is the one I introduced in my post, but makes me feel a bit uncomfortable because it's not documented.

I could think of other approaches then the one introduced with my post such as deleting all the models and the tree view and creating them anew - but this is definitely not as 'smart' as just calling a single method of a class - and moreover there would be some unwanted flickering on the screen.

Thanks for your interest regarding my issue! Maybe you know the ultimate method call...
Ernst

gfunk
12th August 2006, 09:25
Wow, I looked at the source and the initial sort_column value that QSortFilterProxyModel starts with really is -1 ! So you were spot on about "removing sort" by setting to -1. But since it's asserting, probably means that other variables need to be set as well, and I'm not sure what those are. But it would probably work to just delete the QSortFilterProxyModel and reinitialize just that component? Maybe the QSortFilterProxyModel's constructor code or the setSourceModel() code can set the chronological sort properly for you?

3dch
12th August 2006, 15:12
Recreating the QSortFilterProxyModel instance is an option which I'm going to try out. But I can foresee other minor problems arise such as keeping the column widht in the related tree view the same as they have been before - some users for sure dislike when the column width gets adjust "automatically" once they have it set.
Thanks for your advice anyway!
Ernst
:)