diff --git a/src/corelib/itemmodels/qgenericitemmodel.h b/src/corelib/itemmodels/qgenericitemmodel.h index 0f62bd966cd..90c5eadcb39 100644 --- a/src/corelib/itemmodels/qgenericitemmodel.h +++ b/src/corelib/itemmodels/qgenericitemmodel.h @@ -277,7 +277,7 @@ public: readData(*std::next(std::cbegin(row), index.column())); else if constexpr (static_column_count == 0) readData(row); - else + else if (QGenericItemModelDetails::isValid(row)) for_element_at(row, index.column(), readData); } return result; @@ -328,7 +328,7 @@ public: success = writeData(*std::next(std::begin(row), index.column())); } else if constexpr (static_column_count == 0) { success = writeData(row); - } else { + } else if (QGenericItemModelDetails::isValid(row)) { for_element_at(row, index.column(), [&writeData, &success](auto &&target){ using target_type = decltype(target); // we can only assign to an lvalue reference @@ -361,7 +361,7 @@ public: } else if constexpr (static_column_count == 0) { row = row_type{}; success = true; - } else { + } else if (QGenericItemModelDetails::isValid(row)) { for_element_at(row, index.column(), [&success](auto &&target){ using target_type = decltype(target); if constexpr (std::is_lvalue_reference_v diff --git a/src/corelib/itemmodels/qgenericitemmodel_impl.h b/src/corelib/itemmodels/qgenericitemmodel_impl.h index a017f5c94e6..a3397957d29 100644 --- a/src/corelib/itemmodels/qgenericitemmodel_impl.h +++ b/src/corelib/itemmodels/qgenericitemmodel_impl.h @@ -178,6 +178,15 @@ namespace QGenericItemModelDetails template static auto pointerTo(T &t) { return std::addressof(t); } template static auto pointerTo(const T &&t) = delete; + template + static bool isValid(T &&t) + { + if constexpr (std::is_constructible_v) + return bool(t); + else + return true; + } + template auto key(It&& it) -> decltype(it.key()) { return it.key(); }