title: QTableWidget: Prioritize horizontal space for a specific column
body: <p>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>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><a href="https://i.stack.imgur.com/F288R.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/F288R.png" alt="enter image description here" /></a></p>
  <p>The following code shows examples of what I've tried on QTableWidget, but none have worked.  I've provided inline comments on why the following methods do not work:</p>
  <pre><code>table-&gt;horizontalHeader()-&gt;setStretchLastSection(true);
  table-&gt;resizeColumnsToContents();
  </code></pre>
  <p>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 = {&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;};
      table-&gt;setHorizontalHeaderLabels(headers);
  
      table-&gt;setItem(0, 0, new QTableWidgetItem(&quot;1&quot;));
      table-&gt;setItem(0, 1, new QTableWidgetItem(&quot;22222&quot;));
      table-&gt;setItem(0, 2, new QTableWidgetItem(&quot;33333333333333333333333333333&quot;));
      table-&gt;setItem(0, 3, new QTableWidgetItem(&quot;4&quot;));
  
      // 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>