From a52a3b2aa43870e9b9b76cf628e5e666d7ecc457 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Mon, 5 Aug 2019 22:54:33 +0200 Subject: Micro-optimize QAbstractItemModel::setItemData If b becomes false, we won't call setData ever again. Just bail out the loop early and return in that case. Change-Id: I4b0ed7e21546d686bc3f785209a314a8bed08471 Reviewed-by: Volker Hilsheimer Reviewed-by: Marc Mutz --- src/corelib/itemmodels/qabstractitemmodel.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'src/corelib/itemmodels/qabstractitemmodel.cpp') diff --git a/src/corelib/itemmodels/qabstractitemmodel.cpp b/src/corelib/itemmodels/qabstractitemmodel.cpp index c5fb5b9fc5..6e97c2fd39 100644 --- a/src/corelib/itemmodels/qabstractitemmodel.cpp +++ b/src/corelib/itemmodels/qabstractitemmodel.cpp @@ -1898,10 +1898,17 @@ bool QAbstractItemModel::clearItemData(const QModelIndex &index) */ bool QAbstractItemModel::setItemData(const QModelIndex &index, const QMap &roles) { - bool b = true; - for (QMap::ConstIterator it = roles.begin(); it != roles.end(); ++it) - b = b && setData(index, it.value(), it.key()); - return b; + // ### Qt 6: Consider change the semantics of this function, + // or deprecating/removing it altogether. + // + // For instance, it should try setting *all* the data + // in \a roles, and not bail out at the first setData that returns + // false. It should also have a transactional approach. + for (auto it = roles.begin(), e = roles.end(); it != e; ++it) { + if (!setData(index, it.value(), it.key())) + return false; + } + return true; } /*! -- cgit v1.2.3