diff options
author | Christian Ehrlicher <[email protected]> | 2024-04-17 20:55:17 +0200 |
---|---|---|
committer | Christian Ehrlicher <[email protected]> | 2024-05-23 16:52:20 +0200 |
commit | e337f14707d861a74d913a2b890e478532a3fac6 (patch) | |
tree | 257984d5e4b7801718ddf20bfe1228ba39c1e7a6 | |
parent | 434494dfb75ea0248bd9e64dc3426793b442db9f (diff) |
SQL/models: use qsizetype/range-base for loops where possible
Replace some 'native' for loops with range-base for loops or use
qsiztype instead int, mark some helper functions as const.
Change-Id: Ib3f368d6c07bc170ab4ed7cbf28181a226dbeac5
Reviewed-by: Axel Spoerl <[email protected]>
-rw-r--r-- | src/sql/models/qsqlquerymodel.cpp | 7 | ||||
-rw-r--r-- | src/sql/models/qsqlrelationaltablemodel.cpp | 18 | ||||
-rw-r--r-- | src/sql/models/qsqltablemodel.cpp | 33 | ||||
-rw-r--r-- | src/sql/models/qsqltablemodel_p.h | 6 |
4 files changed, 22 insertions, 42 deletions
diff --git a/src/sql/models/qsqlquerymodel.cpp b/src/sql/models/qsqlquerymodel.cpp index 6f91fb97399..1aae088c64a 100644 --- a/src/sql/models/qsqlquerymodel.cpp +++ b/src/sql/models/qsqlquerymodel.cpp @@ -618,7 +618,7 @@ bool QSqlQueryModel::insertColumns(int column, int count, const QModelIndex &par d->colOffsets.append(nVal); Q_ASSERT(d->colOffsets.size() >= d->rec.count()); } - for (int i = column + 1; i < d->colOffsets.size(); ++i) + for (qsizetype i = column + 1; i < d->colOffsets.size(); ++i) ++d->colOffsets[i]; } endInsertColumns(); @@ -644,10 +644,9 @@ bool QSqlQueryModel::removeColumns(int column, int count, const QModelIndex &par beginRemoveColumns(parent, column, column + count - 1); - int i; - for (i = 0; i < count; ++i) + for (int i = 0; i < count; ++i) d->rec.remove(column); - for (i = column; i < d->colOffsets.size(); ++i) + for (qsizetype i = column; i < d->colOffsets.size(); ++i) d->colOffsets[i] -= count; endRemoveColumns(); diff --git a/src/sql/models/qsqlrelationaltablemodel.cpp b/src/sql/models/qsqlrelationaltablemodel.cpp index c086d88ffef..12185147784 100644 --- a/src/sql/models/qsqlrelationaltablemodel.cpp +++ b/src/sql/models/qsqlrelationaltablemodel.cpp @@ -21,7 +21,7 @@ QT_BEGIN_NAMESPACE using namespace Qt::StringLiterals; -class QSqlRelationalTableModelSql: public QSqlTableModelSql +class QSqlRelationalTableModelSql: public QSqlQueryModelSql { public: inline const static QString relTablePrefix(int i) { return QString::number(i).prepend("relTblAl_"_L1); } @@ -108,12 +108,12 @@ struct QRelation void populateModel(); - bool isDictionaryInitialized(); + bool isDictionaryInitialized() const; void populateDictionary(); void clearDictionary(); void clear(); - bool isValid(); + bool isValid() const; QSqlRelation rel; QRelatedTableModel *model; @@ -158,7 +158,7 @@ void QRelation::populateModel() } } -bool QRelation::isDictionaryInitialized() +bool QRelation::isDictionaryInitialized() const { return m_dictInitialized; } @@ -204,7 +204,7 @@ void QRelation::clear() clearDictionary(); } -bool QRelation::isValid() +bool QRelation::isValid() const { return (rel.isValid() && m_parent != nullptr); } @@ -253,10 +253,8 @@ public: void QSqlRelationalTableModelPrivate::clearChanges() { - for (int i = 0; i < relations.size(); ++i) { - QRelation &rel = relations[i]; + for (auto &rel : relations) rel.clear(); - } } void QSqlRelationalTableModelPrivate::revertCachedRow(int row) @@ -277,8 +275,8 @@ int QSqlRelationalTableModelPrivate::nameToIndex(const QString &name) const void QSqlRelationalTableModelPrivate::clearCache() { - for (int i = 0; i < relations.size(); ++i) - relations[i].clearDictionary(); + for (auto &rel : relations) + rel.clearDictionary(); QSqlTableModelPrivate::clearCache(); } diff --git a/src/sql/models/qsqltablemodel.cpp b/src/sql/models/qsqltablemodel.cpp index 265d7782a08..0d171942877 100644 --- a/src/sql/models/qsqltablemodel.cpp +++ b/src/sql/models/qsqltablemodel.cpp @@ -19,12 +19,7 @@ QT_BEGIN_NAMESPACE using namespace Qt::StringLiterals; -using SqlTm = QSqlTableModelSql; - -QSqlTableModelPrivate::~QSqlTableModelPrivate() -{ - -} +using SqlTm = QSqlQueryModelSql; /*! \internal Populates our record with values. @@ -143,11 +138,10 @@ bool QSqlTableModelPrivate::exec(const QString &stmt, bool prepStatement, return false; } } - int i; - for (i = 0; i < rec.count(); ++i) + for (int i = 0; i < rec.count(); ++i) if (rec.isGenerated(i)) editQuery.addBindValue(rec.value(i)); - for (i = 0; i < whereValues.count(); ++i) + for (int i = 0; i < whereValues.count(); ++i) if (whereValues.isGenerated(i) && !whereValues.isNull(i)) editQuery.addBindValue(whereValues.value(i)); @@ -475,10 +469,8 @@ QVariant QSqlTableModel::headerData(int section, Qt::Orientation orientation, in bool QSqlTableModel::isDirty() const { Q_D(const QSqlTableModel); - QSqlTableModelPrivate::CacheMap::ConstIterator i = d->cache.constBegin(); - const QSqlTableModelPrivate::CacheMap::ConstIterator e = d->cache.constEnd(); - for (; i != e; ++i) { - if (!i.value().submitted()) + for (const auto &val : std::as_const(d->cache)) { + if (!val.submitted()) return true; } return false; @@ -1360,8 +1352,7 @@ bool QSqlTableModel::setRecord(int row, const QSqlRecord &values) return false; // Check field names and remember mapping - typedef QMap<int, int> Map; - Map map; + QMap<int, int> map; for (int i = 0; i < values.count(); ++i) { int idx = d->nameToIndex(values.fieldName(i)); if (idx == -1) @@ -1374,18 +1365,16 @@ bool QSqlTableModel::setRecord(int row, const QSqlRecord &values) mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update, QSqlQueryModel::record(row)); - Map::const_iterator i = map.constBegin(); - const Map::const_iterator e = map.constEnd(); - for ( ; i != e; ++i) { + for (const auto i : map.asKeyValueRange()) { // have to use virtual setData() here rather than mrow.setValue() EditStrategy strategy = d->strategy; d->strategy = OnManualSubmit; - QModelIndex cIndex = createIndex(row, i.value()); - setData(cIndex, values.value(i.key())); + QModelIndex cIndex = createIndex(row, i.second); + setData(cIndex, values.value(i.first)); d->strategy = strategy; // setData() sets generated to TRUE, but source record should prevail. - if (!values.isGenerated(i.key())) - mrow.recRef().setGenerated(i.value(), false); + if (!values.isGenerated(i.first)) + mrow.recRef().setGenerated(i.second, false); } if (d->strategy != OnManualSubmit) diff --git a/src/sql/models/qsqltablemodel_p.h b/src/sql/models/qsqltablemodel_p.h index 9c6425ded4b..864c977ebb9 100644 --- a/src/sql/models/qsqltablemodel_p.h +++ b/src/sql/models/qsqltablemodel_p.h @@ -35,7 +35,6 @@ public: strategy(QSqlTableModel::OnRowChange), busyInsertingRows(false) {} - ~QSqlTableModelPrivate(); void clear(); virtual void clearCache(); @@ -154,11 +153,6 @@ public: CacheMap cache; }; -class QSqlTableModelSql: public QSqlQueryModelSql -{ -public: -}; - QT_END_NAMESPACE #endif // QSQLTABLEMODEL_P_H |