Qt QStackedWidget Resizing Issue

北城以北 提交于 2019-12-04 03:26:50

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();
  }
};

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.

step 1: overload method:

void resizeEvent(QResizeEvent*)

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

QRect rect;
rect.setBottomRight(stackedWidget->geometry().bottomRight());
currentWidget->setGeometry(rect);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!