PDA

View Full Version : QAbstactItemView selection after moving rows



craig_d
4th April 2012, 13:01
I have a a model that is used to represent a media player's playlist. As part of this I need to be able to move items in the model (so that a user can re-order a playlist). In my model I call beginMoveRows()/endMoveRows() - and the selection apeears to update on screen (the moved rows keep their selected background). But if the user then shift-clicks elsewhere, the start of the selection appears to be where the original selection was. e.g.:



Moving tracks up:

10 tracks in play list, numbered 1 to 10
Select 8 and 9
Move to under track 3, so we now have 1, 2, 3, 8, 9, 4, 5, 6, 7, 10 (tracks 8 and 9 are still selected)
Shift select track 10
Tracks 8, 6, 7, and 10 are selected - but selection should be 8, 9, 4, 5, 6, 7, 10


Moving tracks down:

10 tracks in play list, numbered 1 to 10
Select 2 and 3
Move to under track 8, so we now have 1, 4, 5, 6, 7, 8, 2, 3, 9, 10 (tracks 2 and 3 are still selected)
Shift select track 4
Only tracks 4 and 2 are selected - but selection should be 4, 5, 6, 7, 8, 2, 3



Reading the QAbstractItemView docs, it states
If the user presses the Shift key while clicking on an item, all items between the current item and the clicked item are selected... So it appears as if the current item is not being updated. I tried to connect to the rowsMoved() signal, and in the slot I checked both the view's, and selection's, current index - both appeared to be fine.

I have some code that attempts to save, and restore, the current index between updates - but this seems a bit too hacky to me. If possible I'dlike to remove this code, and do it properly!

What am I missing?

(The code is part of 'Cantata', which if you are interested you can download from http://kde-apps.org/content/show.php/Cantata?content=147733)

AlekseyOk
4th April 2012, 13:53
try to change selection mode.

craig_d
5th April 2012, 12:54
How does that help? That's completly bypassing the issue.

Looks like this maybe a Qt bug though - as Amarok seems to suffer from the same issue.

Spitfire
9th April 2012, 11:29
Yes, there's a reported bug in Qt (https://bugreports.qt-project.org/browse/QTBUG-18009) as QAbstractItemViewPrivate::pressedPosition is not updated after the drag and drop is performed (and in few other cases like using clearSelection()).
This results in all selection being done based on wrong mouse position.

You can fairly easily walk around the bug like that:


...
connect( this->list->model(), SIGNAL( layoutChanged() ), this, SLOT( correctSelection() ) );
...

void MainWindow::correctSelection( void )
{
QItemSelection s = this->list->selectionModel()->selection();
this->list->setCurrentIndex( this->list->currentIndex() );
this->list->selectionModel()->select( s, QItemSelectionModel::SelectCurrent );
}

I don't think it can be done any better without altering qt source code.

craig_d
16th April 2012, 08:43
Works great! Thanks!