From e85ba9c67fc90a5076a0c00335bd47f17a68d437 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 1 Dec 2015 16:02:57 +0100 Subject: [PATCH] QComboBox: don't re-evaluate count()/model->rowCount() all the time QAbstractItemModel::rowCount() is a potentially expensive operation, so cache its value whereever possible. Change-Id: Ib2829b20a0fedcbf091a535945b88db323b53fbe Reviewed-by: Friedemann Kleint --- src/widgets/widgets/qcombobox.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/widgets/widgets/qcombobox.cpp b/src/widgets/widgets/qcombobox.cpp index 401f365426..565fc7c428 100644 --- a/src/widgets/widgets/qcombobox.cpp +++ b/src/widgets/widgets/qcombobox.cpp @@ -1400,8 +1400,9 @@ void QComboBox::setMaxCount(int max) return; } - if (max < count()) - d->model->removeRows(max, count() - max, d->root); + const int rowCount = count(); + if (rowCount > max) + d->model->removeRows(max, rowCount - max, d->root); d->maxCount = max; } @@ -2585,7 +2586,7 @@ void QComboBox::showPopup() #endif while (!toCheck.isEmpty()) { QModelIndex parent = toCheck.pop(); - for (int i = 0; i < d->model->rowCount(parent); ++i) { + for (int i = 0, end = d->model->rowCount(parent); i < end; ++i) { QModelIndex idx = d->model->index(i, d->modelColumn, parent); if (!idx.isValid()) continue; @@ -3194,6 +3195,8 @@ void QComboBox::keyPressEvent(QKeyEvent *e) } } + const int rowCount = count(); + if (move != NoMove) { e->accept(); switch (move) { @@ -3201,11 +3204,11 @@ void QComboBox::keyPressEvent(QKeyEvent *e) newIndex = -1; case MoveDown: newIndex++; - while ((newIndex < count()) && !(d->model->flags(d->model->index(newIndex,d->modelColumn,d->root)) & Qt::ItemIsEnabled)) + while (newIndex < rowCount && !(d->model->index(newIndex, d->modelColumn, d->root).flags() & Qt::ItemIsEnabled)) newIndex++; break; case MoveLast: - newIndex = count(); + newIndex = rowCount; case MoveUp: newIndex--; while ((newIndex >= 0) && !(d->model->flags(d->model->index(newIndex,d->modelColumn,d->root)) & Qt::ItemIsEnabled)) @@ -3216,7 +3219,7 @@ void QComboBox::keyPressEvent(QKeyEvent *e) break; } - if (newIndex >= 0 && newIndex < count() && newIndex != currentIndex()) { + if (newIndex >= 0 && newIndex < rowCount && newIndex != currentIndex()) { setCurrentIndex(newIndex); d->emitActivated(d->currentIndex); } @@ -3249,6 +3252,7 @@ void QComboBox::wheelEvent(QWheelEvent *e) #else Q_D(QComboBox); if (!d->viewContainer()->isVisible()) { + const int rowCount = count(); int newIndex = currentIndex(); if (e->delta() > 0) { @@ -3257,11 +3261,11 @@ void QComboBox::wheelEvent(QWheelEvent *e) newIndex--; } else if (e->delta() < 0) { newIndex++; - while ((newIndex < count()) && !(d->model->flags(d->model->index(newIndex,d->modelColumn,d->root)) & Qt::ItemIsEnabled)) + while (newIndex < rowCount && !(d->model->index(newIndex, d->modelColumn, d->root).flags() & Qt::ItemIsEnabled)) newIndex++; } - if (newIndex >= 0 && newIndex < count() && newIndex != currentIndex()) { + if (newIndex >= 0 && newIndex < rowCount && newIndex != currentIndex()) { setCurrentIndex(newIndex); d->emitActivated(d->currentIndex); }