PDA

View Full Version : Get source of next row in proxy



supergillis
16th November 2008, 14:46
Hello,

I made a MusicListWidget that keeps all my songs in a QList<Song*>. I use a view and a model to show the songs in the widgets and I use a QSortFilterProxyModel to show them in alphabetic order.

If a song finishes playing (or if I press the next button), I have to play the next song in the list. But the next song in the list isn't the 'real' next song. It's the next song of the proxy.

Here's an illustration:
http://img504.imageshack.us/img504/786/explanationdg2.jpg

So I made this code to find the next song but it crashes...

int MusicListWidget::getNextPosition()
{
// currentPos is the real position of the song in the list
QModelIndex sourceIndex = model->createIndex(currentPos, 0);
QModelIndex proxyIndex = proxy->mapFromSource(sourceIndex);
QModelIndex newProxyIndex = proxy->createIndex(proxyIndex.row()+1, 0);
QModelIndex newModelIndex = proxy->mapToSource(newProxyIndex); // crashed here...
return newModelIndex.row();
}
Can anyone help me out?

Thanks in advance,
Gillis

wysota
16th November 2008, 16:17
I'm sure this code doesn't compile... createIndex() is protected and it is that way for a reason - you should never use it yourself. That's by the way why your code crashes if you made your way around it :) Try this pseudocode:

model = proxy->sourceModel();
nextProxyRow = proxy->index(currentRow+1, 0);
nextSourceRow = proxy->mapToSource(nextProxyRow);

I'm assuming "currentRow" is an integer holding the current row of the view (= the current row of the proxy model).

supergillis
16th November 2008, 16:42
Thanks you very much! The problem was indeed that I was using createIndex (but it did compile because I made friends with the model and the proxy :)). Works like a charm with the index function :)