title: Resizing QTableView section with custom editor
body: <p>I have an application with a <code>QTableView</code> and a model derived from <code>QAbstractItemModel</code>: the first column of the table contains a text (a label for each row), while the second column shows a value that can be selected using a <code>QComboBox</code> created from a custom item delegate. The content of the table may change dynamically (number of rows, language...).</p>
  
  <p>I'd like to resize columns so the second one fits the content and the first one stretches occupying the remaining space.</p>
  
  <p>My first attempt was:</p>
  
  <pre><code>tblData-&gt;horizontalHeader()-&gt;setSectionResizeMode(0, QHeaderView::Stretch);
  tblData-&gt;horizontalHeader()-&gt;setSectionResizeMode(1, QHeaderView::ResizeToContents);
  </code></pre>
  
  <p>Result:</p>
  
  <p><a href="https://i.stack.imgur.com/uhe0W.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uhe0W.png" alt="result1"></a></p>
  
  <p>The problem is that when the <code>QComboBox</code> is selected it doesn't fit the section and it is clipped:</p>
  
  <p><a href="https://i.stack.imgur.com/i6QtL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/i6QtL.png" alt="result2"></a></p>
  
  <p>I've managed to solve this issue by manually increasing the width:</p>
  
  <pre><code>tblData-&gt;horizontalHeader()-&gt;setSectionResizeMode(0, QHeaderView::Stretch);
  tblData-&gt;horizontalHeader()-&gt;setSectionResizeMode(1, QHeaderView::Fixed);
  tblData-&gt;resizeColumnToContents(1);
  tblData-&gt;horizontalHeader()-&gt;resizeSection(1, tblData-&gt;horizontalHeader()-&gt;sectionSize(1) + 40);
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/U0D4s.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/U0D4s.png" alt="result3"></a></p>
  
  <p>Now the issue here is that by using such constant (40 in this case) the width of the section will vary depending on values displayed rather than in all the possible values (if the largest ones are already displayed vs if only the shortest). Also, that constant will be dependant to the style used, since it is also related to the space consumed by the <code>QComboBox</code>.</p>
  
  <p>I've thought about using the <code>Qt::SizeHintRole</code> to manually compute the section width, but it is completely ignored. Even if it was, I cannot compute the actual width of the text (using <code>QFontMetrics::width</code>) because I don't have any font information in the model.</p>
  
  <p>Another approach I've tried is to set the adjust size policy of the <code>QComboBox</code> in the <code>QItemDelegate::createEditor</code> method:</p>
  
  <pre><code>QWidget* myItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem&amp; option, const QModelIndex&amp; index) const {
    auto comboBox = new QComboBox(parent);
    comboBox-&gt;setSizeAdjustPolicy(QComboBox::AdjustToContents);
    // ...
  }
  </code></pre>
  
  <p>But now the combo boxes are either clipped or shortened.</p>
  
  <p><a href="https://i.stack.imgur.com/s72Y9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/s72Y9.png" alt="result4"></a> <a href="https://i.stack.imgur.com/vN2mq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vN2mq.png" alt="result5"></a></p>
  
  <hr>
  
  <p>How can I solve set the section size based on the complete range of content instead of just visible data?</p>
  
  <hr>
  
  <p>I'm self-answering the question with the best approach I've found so far, and the one I'm using in the project right now, but I'm not convinced with it (I detail reasons on the answer) so I'd like to know the correct way to do it. Thanks!</p>