title: How to determine the correct size of a QTableWidget?
body: <p>Is there <em>any</em> way to set the "correct" size of a QTableWidget? (I'm a newbie) This test code is only 25 lines long, in two files, with the file Test.h:</p>
  
  <pre><code>#include &lt;QtGui&gt;
  class Test : public QMainWindow {
     Q_OBJECT
  public:
     Test();
  };
  </code></pre>
  
  <p>and the file Test.cpp:</p>
  
  <pre><code>#include "Test.h"
  Test::Test() : QMainWindow() {
     QVBoxLayout *vbox = new QVBoxLayout;
     QPushButton  *btn = new QPushButton("Hello World etc etc etc etc etc");
     QTableWidget *tbl = new QTableWidget(2, 2);
     vbox-&gt;addWidget(btn);
     vbox-&gt;addWidget(tbl);
     QWidget *w = new QWidget;
     setCentralWidget(w);
     w-&gt;setLayout(vbox);
     resize(1, 1);
  }
  
  int main(int argc, char *argv[]) {
     QApplication app(argc, argv);
     Test test;
     test.show();
     app.exec();
  }
  </code></pre>
  
  <p>Then the command:</p>
  
  <pre><code>   qmake -project &amp;&amp; qmake &amp;&amp; make &amp;&amp; ./Test
  </code></pre>
  
  <p>gives the window:</p>
  
  <p><img src="https://i.stack.imgur.com/OUH94.png" alt="Unwanted window"></p>
  
  <p>But what we want of course is something more like:</p>
  
  <p><img src="https://i.stack.imgur.com/0y7gO.png" alt="Wanted window"></p>
  
  <p>Using <code>tbl-&gt;width()</code> seems to be useless, as it gives a default of <code>640</code> before <code>test.show()</code>, and the unwanted value of <code>195</code> after. I've looked at the Qt Size Hints and Policies until my head spun, and I've tried <code>setResizeMode(QHeaderView::Fixed)</code> and <code>setStretchLastSection(false)</code>. Maybe I'm missing something obvious? This is with Qt 4.7.4 on CentOS 5, if this matters. Thank you for any help.</p>
  
  <p><strong><em>Edit</em></strong>: In response to DK, <em>if</em> the line <code>resize(1, 1);</code> is not present, there is the <em>equal</em> and <em>opposite</em> problem: the window is too large. </p>
  
  <p>And in response to Donotalo, adding:</p>
  
  <pre><code>tbl-&gt;setMaximumWidth(222);
  tbl-&gt;setMinimumWidth(222);
  tbl-&gt;setMaximumHeight(88);
  tbl-&gt;setMinimumHeight(88); 
  </code></pre>
  
  <p>will give the desired window size (at least on my machine), but <em>not</em> in the desired way. How should we calculate the 'constants' <code>222</code> and <code>88?</code></p>
  
  <p>And Ton's answer to <a href="https://stackoverflow.com/a/3996525/318716">Qt: How to force a hidden widget to calculate its layout?</a> doesn't seem to work here: the addition of <code>tbl-&gt;setAttribute(Qt::WA_DontShowOnScreen); tbl-&gt;show();</code> left the value of <code>tbl-&gt;width()</code> unchanged at 640.</p>