4

Using Qt Creator to edit C++11 code for the MinGW build kit.

Each time we iterate over a Qt container it gives this warning:

Loop through Qt object

Is there a better way to write this which doesn't trigger the warning?

Using C++11 range-based for loop correctly in Qt is not an answer - please don't mark as duplicate! This question suggests to use qAsConst; however this doesn't seem to help:

for (const QString &field : qAsConst(fields.keys()))
// - "Call to deleted function qAsConst"

for (const QString &field : qAsConst(fields).keys())
// - no effect, obviously - it was already const

for (const QString &field : std::as_const(fields.keys()))
// - "Call to deleted function as_const"

for (QJsonObject::const_iterator field = fields.constBegin(); field != fields.constEnd(); ++field)
// works, but a bit long-winded
OJW
  • 4,514
  • 6
  • 40
  • 48
  • The code is bad in two ways - first it creates a temporary container by iterating over the `fields` and then the operation on this temporary is not const so detach() is called for every iteration. Use plain iterators instead. – chehrlic May 17 '21 at 18:17

0 Answers0