Use C++11 range-based for instead of Qt foreach.

Switched for loops without all 3 statements to while loops.