Itemviews: avoid unneeded calls to rowCount()

Avoid unneeded calls to rowCount() in a for loop as this might get
expensive for large models.

Change-Id: Ic949ba64f5068b4e6ca0d0c9bc51f181e220da70
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 4280e4c12e289aeb0ed28f07b0036e89bc8fb35f)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Christian Ehrlicher 2024-10-31 18:58:35 +01:00 committed by Qt Cherry-pick Bot
parent fcd52bf984
commit 5f3e64235e
2 changed files with 6 additions and 5 deletions

View File

@ -583,7 +583,8 @@ void QListViewPrivate::selectAll(QItemSelectionModel::SelectionFlags command)
QModelIndex topLeft;
int row = 0;
const int colCount = model->columnCount(root);
for(; row < model->rowCount(root); ++row) {
const int rowCount = model->rowCount(root);
for ( ; row < rowCount; ++row) {
if (isHidden(row)) {
//it might be the end of a selection range
if (topLeft.isValid()) {

View File

@ -498,11 +498,12 @@ void QTableModel::sort(int column, Qt::SortOrder order)
{
QList<QPair<QTableWidgetItem *, int>> sortable;
QList<int> unsortable;
const int numRows = rowCount();
sortable.reserve(rowCount());
unsortable.reserve(rowCount());
sortable.reserve(numRows);
unsortable.reserve(numRows);
for (int row = 0; row < rowCount(); ++row) {
for (int row = 0; row < numRows; ++row) {
if (QTableWidgetItem *itm = item(row, column))
sortable.append(QPair<QTableWidgetItem*,int>(itm, row));
else
@ -515,7 +516,6 @@ void QTableModel::sort(int column, Qt::SortOrder order)
QList<QTableWidgetItem *> sorted_table(tableItems.size());
QModelIndexList from;
QModelIndexList to;
const int numRows = rowCount();
const int numColumns = columnCount();
from.reserve(numRows * numColumns);
to.reserve(numRows * numColumns);