<?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[QStyledItemDelegate doesn&#x27;t  move with its QTreeView column]]></title><description><![CDATA[<p dir="auto">I find that item delegates, assigned to a column via <code>QTreeView</code>, are "sticking" to a particular visual column index, rather than following the logical column as it gets moved around in the model.</p>
<p dir="auto">In my <code>QAbstractItemModel</code>, I insert headers to the front of a list (basically <code>self._headers.insert(0,headerdata)</code> each time).</p>
<p dir="auto">In my <code>QTreeView</code>, I connect <code>self.model().columnsInserted.connect(self._assignDelegates)</code>. In <code>_assignDelegates()</code>,  I loop over the range of logical columns provided by the signal, and based on whatever the particular column is, I call <code>self.setItemDelegateForColumn(col, delegate)</code>.  (In my troubleshooting I've also tried converting the column index to visual via <code>self.header().visualIndex(col)</code>, but this doesn't fix it.)</p>
<p dir="auto">So at this point, the first column of my <code>QTreeView</code> has the delegate assigned.  But as soon as the next column is inserted, I would <em>expect</em> the existing delegate to shift down with its existing column.  But instead, it seems that it "sticks" with visual column 0.  So I'm essentially just always swapping out the delegate on visual column 0 over and over.</p>
<p dir="auto">But then also, if I drag around a column header to visually rearrange the column order in the <code>QTreeView</code>, the delegate follows correctly.  So, geez, pick a behavior.</p>
<p dir="auto">What's the best way of solving this?  Best I can think of is re-applying <em>all</em> delegates to <em>every</em> column <em>every</em> time <em>any</em> columns are inserted or moved in the model.  But that's ridiculously inefficient IMO.</p>
]]></description><link>https://forum.qt.io/topic/163411/qstyleditemdelegate-doesn-t-move-with-its-qtreeview-column</link><generator>RSS for Node</generator><lastBuildDate>Thu, 27 Aug 2026 16:55:07 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/163411.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 02 Oct 2025 22:27:55 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QStyledItemDelegate doesn&#x27;t  move with its QTreeView column on Fri, 03 Oct 2025 05:07:05 GMT]]></title><description><![CDATA[<p dir="auto">Another option: Use a single custom delegate for the entire view, and look up the screen position of the index to determine the desired drawing style.</p>
]]></description><link>https://forum.qt.io/post/832369</link><guid isPermaLink="true">https://forum.qt.io/post/832369</guid><dc:creator><![CDATA[jeremy_k]]></dc:creator><pubDate>Fri, 03 Oct 2025 05:07:05 GMT</pubDate></item><item><title><![CDATA[Reply to QStyledItemDelegate doesn&#x27;t  move with its QTreeView column on Fri, 03 Oct 2025 04:12:30 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/mjiggidyj">@<bdi>mjiggidyj</bdi></a> said in <a href="/post/832365">QStyledItemDelegate doesn't move with its QTreeView column</a>:</p>
<blockquote>
<p dir="auto">In my <code>QAbstractItemModel</code>, I insert headers to the front of a list (basically <code>self._headers.insert(0,headerdata)</code> each time).</p>
<p dir="auto">In my <code>QTreeView</code>, I connect <code>self.model().columnsInserted.connect(self._assignDelegates)</code>. In <code>_assignDelegates()</code>,  I loop over the range of logical columns provided by the signal, and based on whatever the particular column is, I call <code>self.setItemDelegateForColumn(col, delegate)</code>.  (In my troubleshooting I've also tried converting the column index to visual via <code>self.header().visualIndex(col)</code>, but this doesn't fix it.)</p>
</blockquote>
<p dir="auto">This would be a lot easier to follow with a minimal working example, with emphasis on both minimal, and working to demonstrate the issue.</p>
<blockquote>
<p dir="auto">So at this point, the first column of my <code>QTreeView</code> has the delegate assigned.  But as soon as the next column is inserted, I would <em>expect</em> the existing delegate to shift down with its existing column.  But instead, it seems that it "sticks" with visual column 0.  So I'm essentially just always swapping out the delegate on visual column 0 over and over.</p>
<p dir="auto">But then also, if I drag around a column header to visually rearrange the column order in the <code>QTreeView</code>, the delegate follows correctly.  So, geez, pick a behavior.</p>
</blockquote>
<p dir="auto">I don't see an inconsistency. When a delegate for a column is assigned, the column number refers to the column returned by <a href="https://doc.qt.io/qt-6/qmodelindex.html#column" target="_blank" rel="noopener noreferrer nofollow ugc">QModelIndex::column()</a> at the time the delegate is drawn. Inserting a column before a particular item means that the index for that item will have a new column number. The model has been altered, and delegates are used by the view to render the model as it is, not as it was in the past.</p>
<p dir="auto">Dragging the columns in a view around will not result in a new column, because the underlying model hasn't changed.</p>
<blockquote>
<p dir="auto">What's the best way of solving this?  Best I can think of is re-applying <em>all</em> delegates to <em>every</em> column <em>every</em> time <em>any</em> columns are inserted or moved in the model.  But that's ridiculously inefficient IMO.</p>
</blockquote>
<p dir="auto">I don't understand what <strong>this</strong> is. My first thought is to suggest one or more of:</p>
<ul>
<li>a proxy model to maintain the logical column concept</li>
<li>restructuring the model population</li>
<li>waiting for model population to complete before assignment of delegates, or even the view</li>
</ul>
<p dir="auto">Perhaps code, images, or a reference to software implementing the desired interface will help.</p>
]]></description><link>https://forum.qt.io/post/832368</link><guid isPermaLink="true">https://forum.qt.io/post/832368</guid><dc:creator><![CDATA[jeremy_k]]></dc:creator><pubDate>Fri, 03 Oct 2025 04:12:30 GMT</pubDate></item></channel></rss>