PeterG
PeterG

Reputation: 519

Make QTableWidget expand when dialog window size is changed

enter image description hereI have a QTableWidget, QTextEdit, and cancel & okay buttons.

I want these widgets to stay in the same position relative to each other, and the QTableWidget to expand if the dialog window is expanded or size changed...

How can I do that?

Upvotes: 3

Views: 13698

Answers (1)

sam-w
sam-w

Reputation: 7687

You need to look into Qt's layout system - using layouts will handle automatically resizing your objects based on the size of their parent.

A combination of using QWidget::setSizePolicy() and QBoxLayout::setStretch() (or more likely QBoxLayout::insertWidget(..., int stretch = 0, ...)) will allow you to acheive the behaviour you refer to where only certain objects expand to fill available space, while others remain a constant size.

Addressing the image you've given above as an example:

Example of solution in QtDesigner

Aside from dragging and dropping objects into the form, to achieve this solution I have:

  • Set the vertical sizePolicy of textEdit to Fixed.
  • Set a height in textEdit's minimumSize for the sizePolicy to use.
  • Set layoutStretch in centralWidget to 1,0, i.e. assign the minimum possible space for the elements contained in horizontalLayout and give any remaining space to tableWidget.

Upvotes: 13

Related Questions