<table><tr><td style="">meven added a comment.
</td><a style="text-decoration: none; padding: 4px 8px; margin: 0 8px 8px; float: right; color: #464C5C; font-weight: bold; border-radius: 3px; background-color: #F7F7F9; background-image: linear-gradient(to bottom,#fff,#f1f0f1); display: inline-block; border: 1px solid rgba(71,87,120,.2);" href="https://phabricator.kde.org/D27052">View Revision</a></tr></table><br /><div><div><blockquote style="border-left: 3px solid #8C98B8;
color: #6B748C;
font-style: italic;
margin: 4px 0 12px 0;
padding: 8px 12px;
background-color: #F8F9FC;">
<div style="font-style: normal;
padding-bottom: 4px;">In <a href="https://phabricator.kde.org/D27052#603798" style="background-color: #e7e7e7;
border-color: #e7e7e7;
border-radius: 3px;
padding: 0 4px;
font-weight: bold;
color: black;text-decoration: none;">D27052#603798</a>, <a href="https://phabricator.kde.org/p/alex/" style="
border-color: #f1f7ff;
color: #19558d;
background-color: #f1f7ff;
border: 1px solid transparent;
border-radius: 3px;
font-weight: bold;
padding: 0 4px;">@alex</a> wrote:</div>
<div style="margin: 0;
padding: 0;
border: 0;
color: rgb(107, 116, 140);"><p>Hello,<br />
I have a question regarding your changed.</p>
<p>you sometimes declare the variables you want to loop over before the loop, but sometimes in the loop.<br />
For instance in DeviceModel.cpp line 135 it is declared as a const before the loop and in line 139 it is directly used in the loop.</p>
<p>In one of my previous revisions I have implemented it like in line 139 and <a href="https://phabricator.kde.org/D26912?id=74352#inline-152211" class="remarkup-link" target="_blank" rel="noreferrer">https://phabricator.kde.org/D26912?id=74352#inline-152211</a> and changes were requested.</p>
<p>Am I missing conceptual differences between these use cases ?</p>
<p>Thanks again for your expertise !</p></div>
</blockquote>
<p>The important thing is as long as you don't need to change in a loop the container, your container should be const to be used in a for loop, so that semantically the for loop is equivalent to the foreach version, but avoiding the copying foreach did.<br />
So either your container is a variable, it needs to be const, if not use qAsConst, and if it is a function you don't need to do anything if the function is const (<tt style="background: #ebebeb; font-size: 13px;">keys()</tt>), if not the best practice is to introduce an intermediate const variable.</p>
<p>The long version about this Q_FOREACH / foreach is at <a href="https://www.kdab.com/goodbye-q_foreach/" class="remarkup-link" target="_blank" rel="noreferrer">https://www.kdab.com/goodbye-q_foreach/</a></p></div></div><br /><div><strong>REPOSITORY</strong><div><div>R119 Plasma Desktop</div></div></div><br /><div><strong>REVISION DETAIL</strong><div><a href="https://phabricator.kde.org/D27052">https://phabricator.kde.org/D27052</a></div></div><br /><div><strong>To: </strong>meven, broulik, ervin, Plasma<br /><strong>Cc: </strong>alex, plasma-devel, Orage, LeGast00n, The-Feren-OS-Dev, jraleigh, zachus, fbampaloukas, GB_2, ragreen, ZrenBot, ngraham, alexeymin, himcesjf, lesliezhai, ali-mohamed, jensreuterberg, abetts, sebas, apol, ahiemstra, mart<br /></div>