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 <Friedemann.Kleint@theqtcompany.com>
This commit is contained in:
Marc Mutz 2015-12-01 16:02:57 +01:00
parent 1377709711
commit e85ba9c67f

View File

@ -1400,8 +1400,9 @@ void QComboBox::setMaxCount(int max)
return; return;
} }
if (max < count()) const int rowCount = count();
d->model->removeRows(max, count() - max, d->root); if (rowCount > max)
d->model->removeRows(max, rowCount - max, d->root);
d->maxCount = max; d->maxCount = max;
} }
@ -2585,7 +2586,7 @@ void QComboBox::showPopup()
#endif #endif
while (!toCheck.isEmpty()) { while (!toCheck.isEmpty()) {
QModelIndex parent = toCheck.pop(); 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); QModelIndex idx = d->model->index(i, d->modelColumn, parent);
if (!idx.isValid()) if (!idx.isValid())
continue; continue;
@ -3194,6 +3195,8 @@ void QComboBox::keyPressEvent(QKeyEvent *e)
} }
} }
const int rowCount = count();
if (move != NoMove) { if (move != NoMove) {
e->accept(); e->accept();
switch (move) { switch (move) {
@ -3201,11 +3204,11 @@ void QComboBox::keyPressEvent(QKeyEvent *e)
newIndex = -1; newIndex = -1;
case MoveDown: case MoveDown:
newIndex++; 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++; newIndex++;
break; break;
case MoveLast: case MoveLast:
newIndex = count(); newIndex = rowCount;
case MoveUp: case MoveUp:
newIndex--; newIndex--;
while ((newIndex >= 0) && !(d->model->flags(d->model->index(newIndex,d->modelColumn,d->root)) & Qt::ItemIsEnabled)) 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; break;
} }
if (newIndex >= 0 && newIndex < count() && newIndex != currentIndex()) { if (newIndex >= 0 && newIndex < rowCount && newIndex != currentIndex()) {
setCurrentIndex(newIndex); setCurrentIndex(newIndex);
d->emitActivated(d->currentIndex); d->emitActivated(d->currentIndex);
} }
@ -3249,6 +3252,7 @@ void QComboBox::wheelEvent(QWheelEvent *e)
#else #else
Q_D(QComboBox); Q_D(QComboBox);
if (!d->viewContainer()->isVisible()) { if (!d->viewContainer()->isVisible()) {
const int rowCount = count();
int newIndex = currentIndex(); int newIndex = currentIndex();
if (e->delta() > 0) { if (e->delta() > 0) {
@ -3257,11 +3261,11 @@ void QComboBox::wheelEvent(QWheelEvent *e)
newIndex--; newIndex--;
} else if (e->delta() < 0) { } else if (e->delta() < 0) {
newIndex++; 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++; newIndex++;
} }
if (newIndex >= 0 && newIndex < count() && newIndex != currentIndex()) { if (newIndex >= 0 && newIndex < rowCount && newIndex != currentIndex()) {
setCurrentIndex(newIndex); setCurrentIndex(newIndex);
d->emitActivated(d->currentIndex); d->emitActivated(d->currentIndex);
} }