<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Cycling through the items of TreeModel]]></title><description><![CDATA[<p dir="auto">I need to cycle through the items of a TreeModel.<br />
Example:</p>
<p dir="auto">Root<br />
 |_anItem<br />
 |_anotherItem<br />
    |_aChild<br />
    |_anotherChild<br />
 |_andSoOn</p>
<p dir="auto">And I need to browse through them a bit like this:<br />
anItem ; anotherItem ; aChild ; anotherChild ; andSoOn<br />
Is there an iterator going through all the items or do I have to write a kind of "++" operator doing the tests (child is valid and so on)?</p>
<p dir="auto">I'm writing something like that:</p>
<pre><code>void itemIndexPlusPlus(QModelIndex &amp;index){
    int indexRowBuf;
    if (index.child(0,0).isValid())//if has child
        index = index.child(0,0);
    else if (index.parent().child(index.row()+1, 0).isValid())//if has buddy following
        index = index.parent().child(index.row()+1, 0);
    else {
        while ((index != rootIndex())) {//ascend to the master excepted if children of row+1
            indexRowBuf = index.row();
            index = index.parent();
            if (index.child(indexRowBuf + 1, 0).isValid()) {
                index = index.child(indexRowBuf + 1, 0);
                break;
            }
        }
    }
}
</code></pre>
<p dir="auto">But first it's not very stylish : long code and plenty of tests for something simple and then, I don't like to reinvent the wheel. Qt has plenty of wheels, but I sometimes have difficulties to find the good one...</p>
<p dir="auto">Patrick.</p>
]]></description><link>https://forum.qt.io/topic/78996/cycling-through-the-items-of-treemodel</link><generator>RSS for Node</generator><lastBuildDate>Sat, 16 May 2026 04:35:18 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/78996.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 05 May 2017 14:38:44 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Cycling through the items of TreeModel on Tue, 09 May 2017 10:08:31 GMT]]></title><description><![CDATA[<p dir="auto">It's the most permissive way. with all those consts you can call the function with arguments of any constness. for example:</p>
<pre><code class="language-cpp">QAbstractItemModel* model; printModel(treeModel); //works
const QAbstractItemModel* model; printModel(treeModel); //works
QAbstractItemModel* const model; printModel(treeModel); //works
const QAbstractItemModel* const model; printModel(treeModel); //works
</code></pre>
<p dir="auto">Of course I could do that because all I do in that function is print values from model to the console. if you want to change values in the model you have to lose the first <code>const</code> so: <code>void printModel(QAbstractItemModel* const model, const QModelIndex&amp; parent = QModelIndex())</code> but now if you try to call it with a const model (<code>const QAbstractItemModel* model; printModel(treeModel);</code>) it will fail to compile</p>
]]></description><link>https://forum.qt.io/post/392560</link><guid isPermaLink="true">https://forum.qt.io/post/392560</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Tue, 09 May 2017 10:08:31 GMT</pubDate></item><item><title><![CDATA[Reply to Cycling through the items of TreeModel on Tue, 09 May 2017 09:58:35 GMT]]></title><description><![CDATA[<p dir="auto">One question : why are all the arguments <em>const</em> ?</p>
]]></description><link>https://forum.qt.io/post/392559</link><guid isPermaLink="true">https://forum.qt.io/post/392559</guid><dc:creator><![CDATA[Patou355]]></dc:creator><pubDate>Tue, 09 May 2017 09:58:35 GMT</pubDate></item><item><title><![CDATA[Reply to Cycling through the items of TreeModel on Tue, 09 May 2017 07:09:01 GMT]]></title><description><![CDATA[<p dir="auto">It seems to work.</p>
<p dir="auto">Recursion was an old souvenir in my studies...</p>
<p dir="auto">Thanks to you, you contributed to my programmer experience :)</p>
]]></description><link>https://forum.qt.io/post/392505</link><guid isPermaLink="true">https://forum.qt.io/post/392505</guid><dc:creator><![CDATA[Patou355]]></dc:creator><pubDate>Tue, 09 May 2017 07:09:01 GMT</pubDate></item><item><title><![CDATA[Reply to Cycling through the items of TreeModel on Fri, 05 May 2017 17:09:12 GMT]]></title><description><![CDATA[<p dir="auto">How about recursion? for example:</p>
<pre><code class="language-cpp">void printModel(const QAbstractItemModel* const model, const QModelIndex&amp; parent = QModelIndex()) {
  const int rowCount = model-&gt;rowCount(parent);
  if(model-&gt;columnCount(parent) == 0) { return; }
  for(int i = 0; i &lt; rowCount; ++i) {
    const QModelIndex currIdx = model-&gt;index(i, 0, parent);
    qDebug() &lt;&lt; currIdx.data();
    if(model-&gt;hasChildren(currIdx)) {
      printModel(model, currIdx);      
    }
  }
} // printModel
</code></pre>
<p dir="auto">then call it with <code>printModel(treeModel);</code></p>
]]></description><link>https://forum.qt.io/post/391994</link><guid isPermaLink="true">https://forum.qt.io/post/391994</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Fri, 05 May 2017 17:09:12 GMT</pubDate></item></channel></rss>