QAbstractItemView: trigger handlers (and redraw) when changing selection model

The most visible problem is that changing selection model didn't update()
the view, resulting in the old selection/current item still being drawn.
In general trigger the handlers for when the selection/current item
changes.

Change-Id: Ib3b2ad70412e6a21a182d4c173e617710bcc630d
Task-number: QTBUG-50535
Reviewed-by: Stephen Kelly <ske@ableton.com>
bb10
Giuseppe D'Angelo 2016-03-22 11:59:56 +01:00
parent 2119b86db2
commit fce83bd9f8
2 changed files with 63 additions and 0 deletions

View File

@ -765,7 +765,13 @@ void QAbstractItemView::setSelectionModel(QItemSelectionModel *selectionModel)
return;
}
QItemSelection oldSelection;
QModelIndex oldCurrentIndex;
if (d->selectionModel) {
oldSelection = d->selectionModel->selection();
oldCurrentIndex = d->selectionModel->currentIndex();
disconnect(d->selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
this, SLOT(selectionChanged(QItemSelection,QItemSelection)));
disconnect(d->selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
@ -779,6 +785,9 @@ void QAbstractItemView::setSelectionModel(QItemSelectionModel *selectionModel)
this, SLOT(selectionChanged(QItemSelection,QItemSelection)));
connect(d->selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
this, SLOT(currentChanged(QModelIndex,QModelIndex)));
selectionChanged(d->selectionModel->selection(), oldSelection);
currentChanged(d->selectionModel->currentIndex(), oldCurrentIndex);
}
}

View File

@ -37,6 +37,7 @@
#include <qabstractitemview.h>
#include <qstandarditemmodel.h>
#include <qapplication.h>
#include <qevent.h>
#include <qlistview.h>
#include <qlistwidget.h>
#include <qtableview.h>
@ -251,6 +252,7 @@ private slots:
void sizeHintChangeTriggersLayout();
void shiftSelectionAfterChangingModelContents();
void QTBUG48968_reentrant_updateEditorGeometries();
void QTBUG50535_update_on_new_selection_model();
};
class MyAbstractItemDelegate : public QAbstractItemDelegate
@ -2028,5 +2030,57 @@ void tst_QAbstractItemView::QTBUG48968_reentrant_updateEditorGeometries()
// No crash, all fine.
}
void tst_QAbstractItemView::QTBUG50535_update_on_new_selection_model()
{
QStandardItemModel model;
for (int i = 0; i < 10; ++i)
model.appendRow(new QStandardItem(QStringLiteral("%1").arg(i)));
class ListView : public QListView
{
public:
ListView()
: m_paintEventsCount(0)
{
}
int m_paintEventsCount;
protected:
bool viewportEvent(QEvent *event) Q_DECL_OVERRIDE
{
if (event->type() == QEvent::Paint)
++m_paintEventsCount;
return QListView::viewportEvent(event);
}
};
// keep the current/selected row in the "low range", i.e. be sure it's visible, otherwise we
// don't get updates and the test fails.
ListView view;
view.setModel(&model);
view.selectionModel()->setCurrentIndex(model.index(1, 0), QItemSelectionModel::SelectCurrent);
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
QItemSelectionModel selectionModel(&model);
selectionModel.setCurrentIndex(model.index(2, 0), QItemSelectionModel::Current);
int oldPaintEventsCount = view.m_paintEventsCount;
view.setSelectionModel(&selectionModel);
QTRY_VERIFY(view.m_paintEventsCount > oldPaintEventsCount);
QItemSelectionModel selectionModel2(&model);
selectionModel2.select(model.index(0, 0), QItemSelectionModel::ClearAndSelect);
selectionModel2.setCurrentIndex(model.index(1, 0), QItemSelectionModel::Current);
oldPaintEventsCount = view.m_paintEventsCount;
view.setSelectionModel(&selectionModel2);
QTRY_VERIFY(view.m_paintEventsCount > oldPaintEventsCount);
}
QTEST_MAIN(tst_QAbstractItemView)
#include "tst_qabstractitemview.moc"