<?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[QTableWidget: Prioritize horizontal space for a specific column?]]></title><description><![CDATA[<p dir="auto">I have a QTableWidget that has a column (#3) that needs more space than others. I want to resize all columns to their contents, and give priority to column #3. If column #3 pushes the table's width past what's available, I want column #3 to be truncated with '...' without a horizontal scrollBar.</p>
<p dir="auto">The screenshot below is the simplest example of the behavior I'm chasing, but I've had to manually adjust the column widths. I want the table to do this automatically.</p>
<p dir="auto"><img src="https://i.stack.imgur.com/F288R.png" alt="alt text" class=" img-fluid img-markdown" /></p>
<p dir="auto">Note: These functions do not achieve what I'm aiming for.  I want to stretch column 3 to take up the most horizontal space, but if it takes up too much space, then truncate it with elipses (aka "..."), and do not expand the table so that it has a horizontal scrollbar.</p>
<pre><code>table-&gt;horizontalHeader()-&gt;setStretchLastSection(true);
table-&gt;resizeColumnsToContents();
</code></pre>
<p dir="auto">The following code shows examples of what I've tried on QTableWidget, but none have worked.  Thank you to anyone who volunteers your time to help me with this.</p>
<pre><code>#include &lt;QApplication&gt;
#include &lt;QTableWidget&gt;
#include &lt;QStringList&gt;
#include &lt;QRect&gt;
#include &lt;QLayout&gt;
#include &lt;QDialog&gt;
#include &lt;QHeaderView&gt;

#include &lt;iostream&gt;

int main( int argc, char* argv[] )
{
    QApplication a(argc, argv);

    QDialog* d = new QDialog();
    d-&gt;setLayout( new QVBoxLayout() );

    QTableWidget* table = new QTableWidget(1,4);
    QStringList headers = {"1", "2", "3", "4"};
    table-&gt;setHorizontalHeaderLabels(headers);

    table-&gt;setItem(0, 0, new QTableWidgetItem("1"));
    table-&gt;setItem(0, 1, new QTableWidgetItem("22222"));
    table-&gt;setItem(0, 2, new QTableWidgetItem("33333333333333333333333333333"));
    table-&gt;setItem(0, 3, new QTableWidgetItem("4"));

    // Do nothing
    //
    //   The table exceeds the dimensions of the dialog,
    //   and we get a horizontal scrollbar

    // This also results in a horizontal scrollbar
    //
    //  table-&gt;horizontalHeader()-&gt;setStretchLastSection(true);

    // Resizing the columns introduces a horizontal scrollbar, and
    // prevents the user from from changing column width
    //
    //  table-&gt;resizeColumnsToContents();

    // The table fits, but all columns are equally spaced.
    // (We want column 3 to take up as much space as possible)
    //
    // table-&gt;horizontalHeader()-&gt;setSectionResizeMode(QHeaderView::Stretch);

    // Columns are resized to their contents,
    // but column 3 is not truncated and we get a horizontal scrollBar
    //
    //  table-&gt;horizontalHeader()-&gt;setSectionResizeMode(QHeaderView::ResizeToContents);

    d-&gt;layout()-&gt;addWidget( table );
    d-&gt;show();
    return a.exec();
}

</code></pre>
]]></description><link>https://forum.qt.io/topic/124561/qtablewidget-prioritize-horizontal-space-for-a-specific-column</link><generator>RSS for Node</generator><lastBuildDate>Sat, 06 Jun 2026 06:59:46 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/124561.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 09 Mar 2021 00:08:55 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to QTableWidget: Prioritize horizontal space for a specific column? on Tue, 09 Mar 2021 00:31:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pearse">@<bdi>pearse</bdi></a> I hope this works for you</p>
<pre><code> table-&gt;horizontalScrollBar()-&gt;setEnabled(false);

 table-&gt;resizeColumnsToContents();
 
 QHeaderView *headerView = table-&gt;horizontalHeader();
 headerView-&gt;setSectionResizeMode(2, QHeaderView::Stretch);
</code></pre>
]]></description><link>https://forum.qt.io/post/648508</link><guid isPermaLink="true">https://forum.qt.io/post/648508</guid><dc:creator><![CDATA[nagesh]]></dc:creator><pubDate>Tue, 09 Mar 2021 00:31:12 GMT</pubDate></item><item><title><![CDATA[Reply to QTableWidget: Prioritize horizontal space for a specific column? on Wed, 10 Mar 2021 14:11:26 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/nagesh">@<bdi>nagesh</bdi></a> said in <a href="/post/648508">QTableWidget: Prioritize horizontal space for a specific column?</a>:</p>
<blockquote>
<p dir="auto">table-&gt;horizontalScrollBar()-&gt;setEnabled(false);</p>
<p dir="auto">table-&gt;resizeColumnsToContents();</p>
<p dir="auto">QHeaderView *headerView = table-&gt;horizontalHeader();<br />
headerView-&gt;setSectionResizeMode(2, QHeaderView::Stretch);</p>
</blockquote>
<p dir="auto">Thank you</p>
]]></description><link>https://forum.qt.io/post/648871</link><guid isPermaLink="true">https://forum.qt.io/post/648871</guid><dc:creator><![CDATA[pearse]]></dc:creator><pubDate>Wed, 10 Mar 2021 14:11:26 GMT</pubDate></item><item><title><![CDATA[Reply to QTableWidget: Prioritize horizontal space for a specific column? on Tue, 09 Mar 2021 00:31:12 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/pearse">@<bdi>pearse</bdi></a> I hope this works for you</p>
<pre><code> table-&gt;horizontalScrollBar()-&gt;setEnabled(false);

 table-&gt;resizeColumnsToContents();
 
 QHeaderView *headerView = table-&gt;horizontalHeader();
 headerView-&gt;setSectionResizeMode(2, QHeaderView::Stretch);
</code></pre>
]]></description><link>https://forum.qt.io/post/648508</link><guid isPermaLink="true">https://forum.qt.io/post/648508</guid><dc:creator><![CDATA[nagesh]]></dc:creator><pubDate>Tue, 09 Mar 2021 00:31:12 GMT</pubDate></item></channel></rss>