diff options
author | Thierry Bastian <thierry.bastian@nokia.com> | 2009-10-27 14:18:59 (GMT) |
---|---|---|
committer | Thierry Bastian <thierry.bastian@nokia.com> | 2009-10-27 14:20:05 (GMT) |
commit | c79d83b6d661ea49d893b4590480a434d75b19bd (patch) | |
tree | ce3a7ca4528080f7ff6d7ffdb9f4c86083a90d99 | |
parent | 8f040ea4da93f1b9cae1db560e5e9f9b3140b3ff (diff) | |
download | Qt-c79d83b6d661ea49d893b4590480a434d75b19bd.zip Qt-c79d83b6d661ea49d893b4590480a434d75b19bd.tar.gz Qt-c79d83b6d661ea49d893b4590480a434d75b19bd.tar.bz2 |
QComboBox did not emit currentItemChanged when the model was reset
Task-number: QTBUG-569
Reviewed-by: ogoffart
-rw-r--r-- | src/gui/widgets/qcombobox.cpp | 27 | ||||
-rw-r--r-- | src/gui/widgets/qcombobox.h | 3 | ||||
-rw-r--r-- | src/gui/widgets/qcombobox_p.h | 3 | ||||
-rw-r--r-- | tests/auto/qcombobox/tst_qcombobox.cpp | 33 |
4 files changed, 46 insertions, 20 deletions
diff --git a/src/gui/widgets/qcombobox.cpp b/src/gui/widgets/qcombobox.cpp index 0e888d6..7cf35c7 100644 --- a/src/gui/widgets/qcombobox.cpp +++ b/src/gui/widgets/qcombobox.cpp @@ -192,6 +192,8 @@ void QComboBoxPrivate::_q_modelReset() lineEdit->setText(QString()); updateLineEditGeometry(); } + if (currentIndex.row() != indexBeforeChange) + _q_emitCurrentIndexChanged(currentIndex); q->update(); } @@ -992,14 +994,6 @@ void QComboBoxPrivate::_q_dataChanged(const QModelIndex &topLeft, const QModelIn } } -void QComboBoxPrivate::_q_rowsAboutToBeInserted(const QModelIndex & parent, - int /*start*/, int /*end*/) -{ - if (parent != root) - return; - indexBeforeChange = currentIndex.row(); -} - void QComboBoxPrivate::_q_rowsInserted(const QModelIndex &parent, int start, int end) { Q_Q(QComboBox); @@ -1022,11 +1016,8 @@ void QComboBoxPrivate::_q_rowsInserted(const QModelIndex &parent, int start, int } } -void QComboBoxPrivate::_q_rowsAboutToBeRemoved(const QModelIndex &parent, int /*start*/, int /*end*/) +void QComboBoxPrivate::_q_updateIndexBeforeChange() { - if (parent != root) - return; - indexBeforeChange = currentIndex.row(); } @@ -1868,15 +1859,17 @@ void QComboBox::setModel(QAbstractItemModel *model) disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(_q_dataChanged(QModelIndex,QModelIndex))); disconnect(d->model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), - this, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int))); + this, SLOT(_q_updateIndexBeforeChange())); disconnect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(_q_rowsInserted(QModelIndex,int,int))); disconnect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), - this, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int))); + this, SLOT(_q_updateIndexBeforeChange())); disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(_q_rowsRemoved(QModelIndex,int,int))); disconnect(d->model, SIGNAL(destroyed()), this, SLOT(_q_modelDestroyed())); + disconnect(d->model, SIGNAL(modelAboutToBeReset()), + this, SLOT(_q_updateIndexBeforeChange())); disconnect(d->model, SIGNAL(modelReset()), this, SLOT(_q_modelReset())); if (d->model->QObject::parent() == this) @@ -1888,15 +1881,17 @@ void QComboBox::setModel(QAbstractItemModel *model) connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(_q_dataChanged(QModelIndex,QModelIndex))); connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)), - this, SLOT(_q_rowsAboutToBeInserted(QModelIndex,int,int))); + this, SLOT(_q_updateIndexBeforeChange())); connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(_q_rowsInserted(QModelIndex,int,int))); connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)), - this, SLOT(_q_rowsAboutToBeRemoved(QModelIndex,int,int))); + this, SLOT(_q_updateIndexBeforeChange())); connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(_q_rowsRemoved(QModelIndex,int,int))); connect(model, SIGNAL(destroyed()), this, SLOT(_q_modelDestroyed())); + connect(model, SIGNAL(modelAboutToBeReset()), + this, SLOT(_q_updateIndexBeforeChange())); connect(model, SIGNAL(modelReset()), this, SLOT(_q_modelReset())); diff --git a/src/gui/widgets/qcombobox.h b/src/gui/widgets/qcombobox.h index 6a85096..4089a09 100644 --- a/src/gui/widgets/qcombobox.h +++ b/src/gui/widgets/qcombobox.h @@ -308,9 +308,8 @@ private: Q_PRIVATE_SLOT(d_func(), void _q_returnPressed()) Q_PRIVATE_SLOT(d_func(), void _q_resetButton()) Q_PRIVATE_SLOT(d_func(), void _q_dataChanged(const QModelIndex &, const QModelIndex &)) - Q_PRIVATE_SLOT(d_func(), void _q_rowsAboutToBeInserted(const QModelIndex & parent, int start, int end)) + Q_PRIVATE_SLOT(d_func(), void _q_updateIndexBeforeChange()) Q_PRIVATE_SLOT(d_func(), void _q_rowsInserted(const QModelIndex & parent, int start, int end)) - Q_PRIVATE_SLOT(d_func(), void _q_rowsAboutToBeRemoved(const QModelIndex & parent, int start, int end)) Q_PRIVATE_SLOT(d_func(), void _q_rowsRemoved(const QModelIndex & parent, int start, int end)) Q_PRIVATE_SLOT(d_func(), void _q_modelDestroyed()) Q_PRIVATE_SLOT(d_func(), void _q_modelReset()) diff --git a/src/gui/widgets/qcombobox_p.h b/src/gui/widgets/qcombobox_p.h index f7458c4..06e51e4 100644 --- a/src/gui/widgets/qcombobox_p.h +++ b/src/gui/widgets/qcombobox_p.h @@ -357,9 +357,8 @@ public: #endif void _q_resetButton(); void _q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight); - void _q_rowsAboutToBeInserted(const QModelIndex & parent, int start, int end); + void _q_updateIndexBeforeChange(); void _q_rowsInserted(const QModelIndex & parent, int start, int end); - void _q_rowsAboutToBeRemoved(const QModelIndex & parent, int start, int end); void _q_rowsRemoved(const QModelIndex & parent, int start, int end); void updateArrow(QStyle::StateFlag state); bool updateHoverControl(const QPoint &pos); diff --git a/tests/auto/qcombobox/tst_qcombobox.cpp b/tests/auto/qcombobox/tst_qcombobox.cpp index 0d3469d..8acae7a 100644 --- a/tests/auto/qcombobox/tst_qcombobox.cpp +++ b/tests/auto/qcombobox/tst_qcombobox.cpp @@ -152,6 +152,7 @@ private slots: void subControlRectsWithOffset(); void task260974_menuItemRectangleForComboBoxPopup(); void removeItem(); + void resetModel(); protected slots: void onEditTextChanged( const QString &newString ); @@ -2416,5 +2417,37 @@ void tst_QComboBox::removeItem() QCOMPARE(cb.count(), 0); } +void tst_QComboBox::resetModel() +{ + class StringListModel : public QStringListModel + { + public: + StringListModel(const QStringList &list) : QStringListModel(list) + { + } + + void reset() + { + QStringListModel::reset(); + } + }; + QComboBox cb; + StringListModel model( QStringList() << "1" << "2"); + QSignalSpy spy(&cb, SIGNAL(currentIndexChanged(int))); + QCOMPARE(spy.count(), 0); + QCOMPARE(cb.currentIndex(), -1); //no selection + + cb.setModel(&model); + + QCOMPARE(spy.count(), 1); + QCOMPARE(cb.currentIndex(), 0); //first item selected + + model.reset(); + QCOMPARE(spy.count(), 2); + QCOMPARE(cb.currentIndex(), -1); //no selection + +} + + QTEST_MAIN(tst_QComboBox) #include "tst_qcombobox.moc" |