<?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[updating model columns based on view update]]></title><description><![CDATA[<p dir="auto">i have a tree view with 5 column, i created a context menu with checkable items to show/hide columns.</p>
<p dir="auto">i create the model with column names:</p>
<pre><code>class TreeModel : public QAbstractItemModel
{
public:
     explicit TreeModel(QList&lt;QVariant&gt; lstColumnNames, QObject *parent = nullptr) noexcept
            : QAbstractItemModel(parent)
            , m_lstColumnNames(std::move(lstColumnNames))

private:
     	QList&lt;QVariant&gt; m_lstColumnNames;
</code></pre>
<p dir="auto">the reason i do this is to get tree header names and model column count:</p>
<pre><code>QVariant TreeModel::headerData(int section, Qt::Orientation orientation, int role /* = Qt::DisplayRole */) const
{
	return orientation == Qt::Horizontal &amp;&amp; role == Qt::DisplayRole
		? m_lstColumnNames[section]
		: QAbstractItemModel::headerData(section, orientation, role);
}

int TreeModel::columnCount(const QModelIndex &amp;parent /* = QModelIndex() */) const
{
	return parent.isValid()
		? static_cast&lt;TreeNode *&gt;(parent.internalPointer())-&gt;columnCount()
		: m_lstColumnNames.size();
}
</code></pre>
<p dir="auto">now, the problem is that, when i hide a column from context menu (and have 4 columns instead of 5), this list in model is not changed, i.e. <code>columnCount()</code> still returns the old 5.</p>
<p dir="auto">one option i think of is to have list of pairs, <code>QList&lt;QPair&lt;QVariant, bool&gt;&gt; m_lstColumnNames;</code>, where the bool indicates whether the column is hidden or shown. that means i will need to update the column states every time i hide/show column from gui. also, to return column count i'd need to run over all columns and collect only visible ones.</p>
<p dir="auto">i'd like to know what else can i do? probably something better.<br />
thanks</p>
]]></description><link>https://forum.qt.io/topic/116803/updating-model-columns-based-on-view-update</link><generator>RSS for Node</generator><lastBuildDate>Mon, 10 Aug 2026 14:24:28 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/116803.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 08 Jul 2020 14:52:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to updating model columns based on view update on Thu, 09 Jul 2020 15:12:37 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/user4592357">@<bdi>user4592357</bdi></a><br />
I don't understand.  If you allow for allow for hidden columns becoming shown, you have to write the logic to ensure the data is available.  If necessary, react to the column being hidden/shown by refreshing the model or changing it or whatever is required to fetch the necessary data.</p>
]]></description><link>https://forum.qt.io/post/605699</link><guid isPermaLink="true">https://forum.qt.io/post/605699</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Thu, 09 Jul 2020 15:12:37 GMT</pubDate></item><item><title><![CDATA[Reply to updating model columns based on view update on Thu, 09 Jul 2020 05:00:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a><br />
okay, now i add the value of that column in a method filling tree node, only if the column is to be shown, because the computation of that data is slow.<br />
now, if the column is hidden, and i show it, it doesn't display the data (because the logic of filling the node was done somewhere else)</p>
]]></description><link>https://forum.qt.io/post/605674</link><guid isPermaLink="true">https://forum.qt.io/post/605674</guid><dc:creator><![CDATA[user4592357]]></dc:creator><pubDate>Thu, 09 Jul 2020 05:00:43 GMT</pubDate></item><item><title><![CDATA[Reply to updating model columns based on view update on Wed, 08 Jul 2020 19:45:01 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/user4592357">@<bdi>user4592357</bdi></a><br />
In view of <a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a>'s comments, you might consider adding a <code>QProxyModel</code> which hides the computed column from the outside world, makes life simpler.</p>
]]></description><link>https://forum.qt.io/post/605632</link><guid isPermaLink="true">https://forum.qt.io/post/605632</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Wed, 08 Jul 2020 19:45:01 GMT</pubDate></item><item><title><![CDATA[Reply to updating model columns based on view update on Wed, 08 Jul 2020 17:57:08 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/user4592357">@<bdi>user4592357</bdi></a> said in <a href="/post/605618">updating model columns based on view update</a>:</p>
<blockquote>
<p dir="auto">then where?</p>
</blockquote>
<p dir="auto">When you fill the data into the model for example. data() is called very often and should do as less work as possible.</p>
]]></description><link>https://forum.qt.io/post/605622</link><guid isPermaLink="true">https://forum.qt.io/post/605622</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Wed, 08 Jul 2020 17:57:08 GMT</pubDate></item><item><title><![CDATA[Reply to updating model columns based on view update on Wed, 08 Jul 2020 17:46:44 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a><br />
then where? if <code>data()</code> wants to retrieve that model index info, i need to provide the correct data, since i don't know whether column is hidden in gui or not</p>
]]></description><link>https://forum.qt.io/post/605618</link><guid isPermaLink="true">https://forum.qt.io/post/605618</guid><dc:creator><![CDATA[user4592357]]></dc:creator><pubDate>Wed, 08 Jul 2020 17:46:44 GMT</pubDate></item><item><title><![CDATA[Reply to updating model columns based on view update on Wed, 08 Jul 2020 17:44:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/user4592357">@<bdi>user4592357</bdi></a> You should not do the calculation inside the data() function.</p>
]]></description><link>https://forum.qt.io/post/605616</link><guid isPermaLink="true">https://forum.qt.io/post/605616</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Wed, 08 Jul 2020 17:44:43 GMT</pubDate></item><item><title><![CDATA[Reply to updating model columns based on view update on Wed, 08 Jul 2020 17:43:31 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a><br />
i have one column, which does some heavy calculation for data retrieval, so i wouldn't want extra work be done</p>
]]></description><link>https://forum.qt.io/post/605614</link><guid isPermaLink="true">https://forum.qt.io/post/605614</guid><dc:creator><![CDATA[user4592357]]></dc:creator><pubDate>Wed, 08 Jul 2020 17:43:31 GMT</pubDate></item><item><title><![CDATA[Reply to updating model columns based on view update on Wed, 08 Jul 2020 17:39:15 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/user4592357">@<bdi>user4592357</bdi></a> Why is this a problem?</p>
]]></description><link>https://forum.qt.io/post/605610</link><guid isPermaLink="true">https://forum.qt.io/post/605610</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Wed, 08 Jul 2020 17:39:15 GMT</pubDate></item><item><title><![CDATA[Reply to updating model columns based on view update on Wed, 08 Jul 2020 16:11:39 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> said in <a href="/post/605576">updating model columns based on view update</a>:</p>
<blockquote>
<p dir="auto">Why should the model change it's columnCount at all?</p>
</blockquote>
<p dir="auto">the problem is that, since model column count is the same, <code>data()</code> is called for those hidden columns.</p>
]]></description><link>https://forum.qt.io/post/605581</link><guid isPermaLink="true">https://forum.qt.io/post/605581</guid><dc:creator><![CDATA[user4592357]]></dc:creator><pubDate>Wed, 08 Jul 2020 16:11:39 GMT</pubDate></item><item><title><![CDATA[Reply to updating model columns based on view update on Wed, 08 Jul 2020 15:57:31 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/user4592357">@<bdi>user4592357</bdi></a> said in <a href="/post/605564">updating model columns based on view update</a>:</p>
<blockquote>
<p dir="auto">this list in model is not changed, i.e. columnCount() still returns the old 5.</p>
</blockquote>
<p dir="auto">That's absolutely correct - you only hide a column in the view, you don't change the model. Why should the model change it's columnCount at all?</p>
]]></description><link>https://forum.qt.io/post/605576</link><guid isPermaLink="true">https://forum.qt.io/post/605576</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Wed, 08 Jul 2020 15:57:31 GMT</pubDate></item></channel></rss>