3

I have a QStackWidget with 2 pages. One page contains just 2 QLineEdit widget and another page contains a large QTextEdit. Does anyone know how to make one page larger than another so that the window still fits tightly for each page? In the Qt Designer, the QStackWidget has to take the larger size in order to first the QTextEdit, but then the page with the QLineEdit widgets would be way too big.

Thanks

Nejat
  • 31,784
  • 12
  • 106
  • 138
Kar
  • 6,063
  • 7
  • 53
  • 82

1 Answers1

4

You can set the size policy of the page that is displayed to QSizePolicy.Preferred and the other ones to QSizePolicy.Ignored. After that call adjustSize to update the sizes. For example when you set the current index to show page1 :

page1.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
page2.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)

stackedWidget.adjustSize()
adjustSize()

And when the other page is shown, set the size policies the other way.

Nejat
  • 31,784
  • 12
  • 106
  • 138
  • But then, how does the stacked widget take the size of the smaller or larger page? If I use `QWidget` for the pages, `sizehint()` returns -1 -1 – Kar Feb 19 '15 at 09:45
  • try `resize(minimumSizeHint());`. This would shrink it to minimum size. – Nejat Feb 19 '15 at 09:54
  • Right. If I print `page1.minimumSizeHint()` (likewise for `page2`), I get `QSize(-1, -1)`. Should the pages be of class `QWidget`? – Kar Feb 19 '15 at 10:00
  • It can be any widget. Just should be derived from `QWidget`. – Nejat Feb 19 '15 at 10:06