QListWidget: setup connections when changing selection model.

QListWidget uses a set of slots for its selection model that are
connected only at creation time. This patch adds the missing
connections cleanup and setup when a user changes the selection
model.

Task-number: QTBUG-50891
Change-Id: I942bae6c471ea1ae22637d09b96d6fbd422f653f
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
bb10
Samuel Gaist 2016-02-20 22:50:23 +01:00
parent 923be3f78c
commit 6129aade00
3 changed files with 53 additions and 4 deletions

View File

@ -1069,10 +1069,6 @@ void QListWidgetPrivate::setup()
QObject::connect(q, SIGNAL(entered(QModelIndex)), q, SLOT(_q_emitItemEntered(QModelIndex)));
QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
q, SLOT(_q_emitItemChanged(QModelIndex)));
QObject::connect(q->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
q, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex)));
QObject::connect(q->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
q, SIGNAL(itemSelectionChanged()));
QObject::connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
q, SLOT(_q_dataChanged(QModelIndex,QModelIndex)));
QObject::connect(model, SIGNAL(columnsRemoved(QModelIndex,int,int)), q, SLOT(_q_sort()));
@ -1354,6 +1350,31 @@ QListWidget::~QListWidget()
{
}
/*!
\reimp
*/
void QListWidget::setSelectionModel(QItemSelectionModel *selectionModel)
{
Q_D(QListWidget);
if (d->selectionModel) {
QObject::disconnect(d->selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
this, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex)));
QObject::disconnect(d->selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
this, SIGNAL(itemSelectionChanged()));
}
QListView::setSelectionModel(selectionModel);
if (d->selectionModel) {
QObject::connect(d->selectionModel, SIGNAL(currentChanged(QModelIndex,QModelIndex)),
this, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex)));
QObject::connect(d->selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
this, SIGNAL(itemSelectionChanged()));
}
}
/*!
Returns the item that occupies the given \a row in the list if one has been
set; otherwise returns 0.

View File

@ -207,6 +207,8 @@ public:
explicit QListWidget(QWidget *parent = Q_NULLPTR);
~QListWidget();
void setSelectionModel(QItemSelectionModel *selectionModel) Q_DECL_OVERRIDE;
QListWidgetItem *item(int row) const;
int row(const QListWidgetItem *item) const;
void insertItem(int row, QListWidgetItem *item);

View File

@ -115,6 +115,7 @@ private slots:
void QTBUG8086_currentItemChangedOnClick();
void QTBUG14363_completerWithAnyKeyPressedEditTriggers();
void mimeData();
void QTBUG50891_ensureSelectionModelSignalConnectionsAreSet();
protected slots:
void rowsAboutToBeInserted(const QModelIndex &parent, int first, int last)
@ -1700,5 +1701,30 @@ void tst_QListWidget::mimeData()
delete data2;
}
void tst_QListWidget::QTBUG50891_ensureSelectionModelSignalConnectionsAreSet()
{
qRegisterMetaType<QListWidgetItem*>("QListWidgetItem*");
QListWidget list;
for (int i = 0 ; i < 4; ++i)
new QListWidgetItem(QString::number(i), &list);
list.setSelectionModel(new QItemSelectionModel(list.model()));
list.show();
QSignalSpy currentItemChangedSpy(&list, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)));
QSignalSpy itemSelectionChangedSpy(&list, SIGNAL(itemSelectionChanged()));
QVERIFY(QTest::qWaitForWindowExposed(&list));
QCOMPARE(currentItemChangedSpy.count(), 0);
QCOMPARE(itemSelectionChangedSpy.count(), 0);
QTest::mouseClick(list.viewport(), Qt::LeftButton, 0, list.visualItemRect(list.item(2)).center());
QCOMPARE(currentItemChangedSpy.count(), 1);
QCOMPARE(itemSelectionChangedSpy.count(), 1);
}
QTEST_MAIN(tst_QListWidget)
#include "tst_qlistwidget.moc"