Emit the highlighted signal if the model changes.
The bug is that the connection to emit that signal can be made obsolete if the connection is made too early and the model is replaced. In the bug report, the connection is made by calling view() early (thereby causing the creation of a view and a QItemSelectionModel which operates on the built-in QItemSelectionModel, and then connecting to that QItemSelectionModel), and then when QComboBox::setModel() is called later the built-in view creates a new QItemSelectionModel for it. The bug was that that new QItemSelectionModel is not connected to. This patch fixes that bug. Task-number: QTBUG-4454 Change-Id: Ibbdb8731f16ab071008b4a19dc2cc7ae03cebc84 Reviewed-by: Marc Mutz <marc.mutz@kdab.com> Reviewed-by: Stephen Kelly <stephen.kelly@kdab.com>bb10
parent
61fa926857
commit
351904be50
|
|
@ -1914,8 +1914,12 @@ void QComboBox::setModel(QAbstractItemModel *model)
|
|||
connect(model, SIGNAL(modelReset()),
|
||||
this, SLOT(_q_modelReset()));
|
||||
|
||||
if (d->container)
|
||||
if (d->container) {
|
||||
d->container->itemView()->setModel(model);
|
||||
connect(d->container->itemView()->selectionModel(),
|
||||
SIGNAL(currentChanged(QModelIndex,QModelIndex)),
|
||||
this, SLOT(_q_emitHighlighted(QModelIndex)), Qt::UniqueConnection);
|
||||
}
|
||||
|
||||
bool currentReset = false;
|
||||
|
||||
|
|
|
|||
|
|
@ -158,6 +158,7 @@ private slots:
|
|||
void task_QTBUG_1071_changingFocusEmitsActivated();
|
||||
void maxVisibleItems();
|
||||
void task_QTBUG_10491_currentIndexAndModelColumn();
|
||||
void highlightedSignal();
|
||||
|
||||
protected slots:
|
||||
void onEditTextChanged( const QString &newString );
|
||||
|
|
@ -2565,5 +2566,33 @@ void tst_QComboBox::task_QTBUG_10491_currentIndexAndModelColumn()
|
|||
QCOMPARE(QModelIndex(d->currentIndex), model.index(2, comboBox.modelColumn()));
|
||||
}
|
||||
|
||||
void tst_QComboBox::highlightedSignal()
|
||||
{
|
||||
QComboBox comboBox;
|
||||
|
||||
QSignalSpy spy(&comboBox, SIGNAL(highlighted(int)));
|
||||
QVERIFY(spy.isValid());
|
||||
|
||||
// Calling view() before setting the model causes the creation
|
||||
// of a QComboBoxPrivateContainer containing an actual view, and connecting to
|
||||
// the selectionModel to generate the highlighted signal. When setModel is called
|
||||
// further down, that selectionModel is obsolete. We test that the highlighted
|
||||
// signal is emitted anyway as the bug fix. (QTBUG-4454)
|
||||
comboBox.view();
|
||||
QItemSelectionModel *initialItemSelectionModel = comboBox.view()->selectionModel();
|
||||
|
||||
|
||||
QStandardItemModel model;
|
||||
for (int i = 0; i < 5; i++)
|
||||
model.appendRow(new QStandardItem(QString::number(i)));
|
||||
comboBox.setModel(&model);
|
||||
|
||||
comboBox.view()->selectionModel()->setCurrentIndex(model.index(0, 0), QItemSelectionModel::Current);
|
||||
|
||||
QVERIFY(initialItemSelectionModel != comboBox.view()->selectionModel());
|
||||
|
||||
QCOMPARE(spy.size(), 1);
|
||||
}
|
||||
|
||||
QTEST_MAIN(tst_QComboBox)
|
||||
#include "tst_qcombobox.moc"
|
||||
|
|
|
|||
Loading…
Reference in New Issue