<?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[cannot get fixed sized columns in QTableView to work.]]></title><description><![CDATA[<p dir="auto">I am having trouble initializing a QTableView with fixed column header column widths.</p>
<p dir="auto">When I display the table for the first time, the columns seem to use the default <code>horizontalHeaderDefaultSectionSize</code> property from the Qt Designer's property editor.</p>
<p dir="auto">I also have the <code>horizontalHeaderStretchLastSection</code> property set to <code>true</code>.</p>
<p dir="auto">If I double-click on each of the columns, the layout snaps to the correct width, however I have to do this for each of the columns (except the last which is set to stretch as I just described).  I am not sure why the initial layout does not respect the widths in my code.</p>
<p dir="auto">Has this something to do with <code>sizeHint</code>s in a model for a <code>horizontalHeader</code>?  I do not want to automatically size to the contents of the table (as the table can grow to a very large size, I just want the initial sizes to be correct).</p>
<p dir="auto">I set up my views using a table driven approach. The table contains a sample fixed size string or a length that I use to size the header (using font metrics):</p>
<pre><code class="language-c++">using TableSpec = std::vector&lt;
    std::tuple&lt;
        QTableView*,
        QStandardItemModel*,
        std::vector&lt;TableColumnSpec&gt;
    &gt;
&gt;;

Define the layout for tables in the main window.
// Views with proxy sort filters need special handling
// as the view does not directly have access to the model.
const TableSpec gTableSpecs = { {
    // Thread colored logger.
    {
        ui-&gt;logView,
        mpLogViewModel.get(),
        {
            TableColumnSpec("Time", "Timestamp", "06-09-2023 20:01:33.334"),
            TableColumnSpec("Thread", "Thread ID", "0x00000000"),
            TableColumnSpec("Level", "Severity level", "critical"),
            TableColumnSpec("Description", "Description", "description")
        }
    },
} };
</code></pre>
<p dir="auto">The above code shows just one of my tables (the one that shows a log table).  This table uses a custom delegate that is assigned as follows (I don't think the delegate is involved with laying out the initial view of the horizontal header though), I also assign the <code>mpLogViewModel</code> (<code>std::unique_ptr&lt;QStandardItemModel&gt;</code>) to the view from here:</p>
<pre><code class="language-c++">// Assign item delegate to color the rows using the threadID (column1).
ui-&gt;logView-&gt;setItemDelegate(mpLogItemDelegate.get());
// Associate each model with its respective view.
ui-&gt;logView-&gt;setModel(mpLogViewModel.get());
</code></pre>
<p dir="auto">Now that the item delegate and model have been associated with the <code>ui-&gt;logView</code> (QTableView), I initialize the table(s) as follows:</p>
<pre><code class="language-c++">// Layout the main application tables &amp; headers.
initTableHeaders(gTableSpecs);

void
initTableHeaders(const TableSpec&amp; rTableSpec)
{
    for (const auto&amp; [pView, pModel, colSpecs] : rTableSpec) {
        const auto fm = pView-&gt;fontMetrics();
        const auto rowHeight = fm.lineSpacing();
        pView-&gt;verticalHeader()-&gt;setDefaultSectionSize(rowHeight);
        pView-&gt;verticalHeader()-&gt;setMaximumSectionSize(rowHeight);
        pView-&gt;verticalHeader()-&gt;setMinimumSectionSize(rowHeight);
        pView-&gt;verticalHeader()-&gt;setVisible(false);
        pView-&gt;horizontalHeader()-&gt;setMinimumSectionSize(25);
        pView-&gt;horizontalHeader()-&gt;setDefaultAlignment(Qt::AlignLeft);
        pView-&gt;horizontalHeader()-&gt;setStretchLastSection(true);
        pView-&gt;horizontalHeader()-&gt;setSortIndicator(0, Qt::AscendingOrder);
        pView-&gt;horizontalHeader()-&gt;setSortIndicatorShown(true);
        pView-&gt;setSelectionBehavior(QAbstractItemView::SelectRows);
        for (auto col = 0; col &lt; static_cast&lt;int&gt;(colSpecs.size()); ++col) {
            const auto headerLabel = new QStandardItem(colSpecs[col].mColumnTitle);
            headerLabel-&gt;setTextAlignment(colSpecs[col].mAlignment);
            pModel-&gt;setHorizontalHeaderItem(col, headerLabel);
            const auto columnWidth = std::visit(Overload{
                [&amp;](const QString&amp; rSampleStr)-&gt;int {
                    if (!rSampleStr.isEmpty()) {
                        return fm.horizontalAdvance(rSampleStr);
                    }
                    return fm.horizontalAdvance(colSpecs[col].mColumnTitle);
                },
                [&amp;](const auto stringLength)-&gt;int {
                    if (stringLength &gt; 0) {
                        return stringLength * fm.averageCharWidth();
                    }
                    return fm.horizontalAdvance(colSpecs[col].mColumnTitle);
                },
            }, colSpecs[col].mSizeInfo);
            // Add 20 to make room for the sort arrow.
            pView-&gt;setColumnWidth(col, columnWidth + 20);
        }
    }
}
</code></pre>
<p dir="auto">The problem is that when the log is displayed, it looks like this:</p>
<p dir="auto"><a href="https://i.stack.imgur.com/rxaMW.png" target="_blank" rel="noopener noreferrer nofollow ugc"><img src="https://i.stack.imgur.com/rxaMW.png" alt="enter image description here" class=" img-fluid img-markdown" /></a></p>
]]></description><link>https://forum.qt.io/topic/154881/cannot-get-fixed-sized-columns-in-qtableview-to-work</link><generator>RSS for Node</generator><lastBuildDate>Sun, 19 Jul 2026 11:51:23 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/154881.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 24 Feb 2024 21:05:57 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to cannot get fixed sized columns in QTableView to work. on Sun, 25 Feb 2024 10:52:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/johnco3">@<bdi>johnco3</bdi></a> said in <a href="/post/791284">cannot get fixed sized columns in QTableView to work.</a>:</p>
<blockquote>
<p dir="auto">perhaps it has to do with settings fixed on the last column which really needs to be Stretch to use the remaining horizontal space?</p>
</blockquote>
<p dir="auto">Yes, it does. I think to make it really a fixed size you might need to set the minimum and maximum size to the same thing.</p>
]]></description><link>https://forum.qt.io/post/791305</link><guid isPermaLink="true">https://forum.qt.io/post/791305</guid><dc:creator><![CDATA[kshegunov]]></dc:creator><pubDate>Sun, 25 Feb 2024 10:52:12 GMT</pubDate></item><item><title><![CDATA[Reply to cannot get fixed sized columns in QTableView to work. on Sun, 25 Feb 2024 04:24:18 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/kshegunov">@<bdi>kshegunov</bdi></a> said in <a href="/post/791272">cannot get fixed sized columns in QTableView to work.</a>:</p>
<blockquote>
<p dir="auto">header-&gt;resizeSection(col, columnWidth + 20);</p>
</blockquote>
<p dir="auto">I just tried this, unfortunately it does not work - but thank you, as after changing from 'Fixed' to 'ResizeToContents', the initial layout accept ed my column widths (strange that it would not accept Fixed - perhaps it has to do with settings fixed on the last column which really needs to be Stretch to use the remaining horizontal space?  My issue was not resizing the section on the view's horizontalHeader.  Thanks!!!</p>
<pre><code>// Add 20 to make room for the sort arrow.
const auto header = pView-&gt;horizontalHeader();
header-&gt;setSectionResizeMode(col, QHeaderView::ResizeToContents);
header-&gt;resizeSection(col, columnWidth + 20);
</code></pre>
]]></description><link>https://forum.qt.io/post/791284</link><guid isPermaLink="true">https://forum.qt.io/post/791284</guid><dc:creator><![CDATA[johnco3]]></dc:creator><pubDate>Sun, 25 Feb 2024 04:24:18 GMT</pubDate></item><item><title><![CDATA[Reply to cannot get fixed sized columns in QTableView to work. on Sun, 25 Feb 2024 00:21:36 GMT]]></title><description><![CDATA[<pre><code>// [snip ...]
// Add 20 to make room for the sort arrow.
// pView-&gt;setColumnWidth(col, columnWidth + 20);
auto header = pView-&gt;horizontalHeader();
header-&gt;setSectionResizeMode(col, QHeaderView::Fixed);
header-&gt;resizeSection(col, columnWidth + 20);
</code></pre>
]]></description><link>https://forum.qt.io/post/791272</link><guid isPermaLink="true">https://forum.qt.io/post/791272</guid><dc:creator><![CDATA[kshegunov]]></dc:creator><pubDate>Sun, 25 Feb 2024 00:21:36 GMT</pubDate></item></channel></rss>