10

I have a simple QStackedWidget with 3 different QWidgets in it. The minimum sizes of the QWidgets are (350x200), (200x100), and (450x450).

So the problem I'm having is when I resize the QStackedWidget, it has a minimum size of the largest QWidget within it. So if I switch to the second QWidget, (which is the QWidget with the size of (200x100)), the QStackedWidget will only size down (450x450) because of the largest QWidget inside of it. I would like it to size down to fit the current QWidget being displayed, and remove that minimum size.

tshepang
  • 12,111
  • 21
  • 91
  • 136
David Ludwig
  • 967
  • 2
  • 12
  • 26
  • This question was already asked [here](http://stackoverflow.com/questions/14480696/qstackedwidget-different-contents-same-size?rq=1) and I saw it after posting. the linked question had a lower quality, so I edited it and I vote for closing this one as duplicate. – UmNyobe May 07 '14 at 08:20
  • Alright thanks bro. Been looking around but didn't see this – David Ludwig May 07 '14 at 08:24
  • The title of that question was incorrect. You had low chances of reading it even if showed up in search result. – UmNyobe May 07 '14 at 08:26

4 Answers4

8

I think that the most straightforward solution is to subclass QStackedWidget and override sizeHint and minimumSizeHint to return the hint for the current widget instead of the maximum of all widgets. For example:

class StackedWidget : public QStackedWidget
{
  QSize sizeHint() const override
  {
    return currentWidget()->sizeHint();
  }

  QSize minimumSizeHint() const override
  {
    return currentWidget()->minimumSizeHint();
  }
};
Jason Haslam
  • 2,617
  • 13
  • 19
6

Good question. Unfortunately Qt doesnt provide automatic mechanisms for sizing down depending on child widgets (By automatic I mean you don't have to do anything). Most of the focus is on expanding (see the size policies)

You have two options :

  1. Use the signal and slots mechanism in the class which create those widgets. You will need to listen to the signal void QStackedWidget ::currentChanged ( int index ) and resize the stackedwidget to the size of the widget at index. This is quite fast to code.

  2. Decorate QStackedWidget and define the size properties. Basically both sizeHint() and minimumSizeHint() should return the size of the current widget. Addwidget(QWidget*) also need to be modified. Useful if you are using stacked widgets everywhere.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
0

step 1: overload method:

void resizeEvent(QResizeEvent*)

step 2: call 'resize' and 'select page' :

QRect rect;
rect.setBottomRight(stackedWidget->geometry().bottomRight());
currentWidget->setGeometry(rect);
Mr.Angel
  • 117
  • 1
  • 4
  • This only works if the QStackedWidget itself is not in a layout. If it's in a layout then you must set its size via size hint, minimum size, size policy etc. The answers to the other question, http://stackoverflow.com/questions/14480696/resize-qstackedwidget-to-the-page-which-is-opened (marked as a duplicate) are more useful. – Hamish Moffatt Nov 19 '14 at 04:17
0

An alternate solution is to not use stacked widget at all and instead make one Widget for each page and then use the setVisible(bool) to show and hide the pages. That way you get the resizing behavior without having to make custom widgets.