BACKGROUND

I have a widget, MyWidget, deriving from QWidget. MyWidget contains a class, MyTableView, which derives from QTableView which is in a QVBoxLayout.

MyWidget has setWindowFlags(Qt::Tool) so it is displayed in a window even though it's a child of QMainWindow.

MyTableView has sizeHint overidden to pass back the size of the actual table view based on its headers:

Qt Code:
  1. return QSize(horizontalHeader()->sizeHint().width(), verticalHeader()->sizeHint().width());
To copy to clipboard, switch view to plain text mode 

This means that MyTableView is only as wide as its horizontal header and will change its width along with the horizontal header. The user is able to change the width of MyTableView by changing the width of the columns.

DESIRED BEHAVIOUR

I want the width of the window to expand and contract along with the table view. But I also want the user to be able to change the height of the window. So, the window's width is dictated by the width of the widget, but the window should control its own height or allow its height to be changed by the user.

PROBLEM

If I call MyWidget::layout()->setSizeConstraint(QLayout::SetFixedSize) the layout responds to sizeHint of the widget. This works perfectly for width - as MyTableWidget's width changes the window changes to fit. But this means that the height of the window is determined by the widget's sizeHint too. MyTableView can contain hundreds of rows, so this doesn't work. Is there a way to have SetFixedSize apply only to width for a layout?

I have tried every combination that I can think of (sizeConstraints, sizePolicy, etc.), but I just can't seem to get this working the way I would like it to work.

Does anyone have any ideas? If there was a way to resize the height of MyTableView that would solve the problem, but that would involve resizing the viewport and I don't know how to do that or if it's even possible.