相关文章推荐
没有腹肌的鸵鸟  ·  Workbook.RefreshAll ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to get a widget placed on a page in a QStackedWidget to resize automatically when the parent QStackedWidget resizes. This simple task seems impossible to do with Qt Creator. Adding a layout to the page does not seem to do anything, the layout just stays at whatever size it's designed as, and will not resize. This is (I think?) because the layout is being added into the page widget as a child and not directly attached to the page widget. There seems to be no way to actually add a layout to page.

How do a get a widget to resize in a QStackedWidget with Qt Creator???

As a side comment, I really wish Qt would abandon it's layout system, it is far far harder to use then an anchoring based system.

Are you using the Qt3 support widget ( QWidgetStack ) or the new Qt4 widget ( QStackedWidget )? RA. Nov 6, 2012 at 23:16

Qt Creator treats widgets as if they already have a layout on them which you can access from the context menu (right-click) of the widget in the designer or from the widget list at the right. This means that you only have to drop layouts (from the palette) on a widget if you want to divide that top level layout into further sub layouts. The image below shows this context menu.

Notice the widget icons on the right that show a crossed-out red circle. This indicates that no top-level layout has been defined for those widgets. Once a layout type has been chosen, the red circle will disappear and the icon will reflect your layout choice.

In your case, after you drop a QStackedWidget on your main widget, you can right-click the main widget, select "Lay out" and choose "Lay Out Horizontally". This will make the stacked widget fill the entire main widget and resize with it.

Likewise, if you drop any widgets on one of the "page" widgets, you can then right-click the page widget and assign a top level layout for that which will cause those widgets to resize accordingly with the page widget.

Arnold, Thanks for the answer. I have added a layout to each page and to the parent stack and that has fixed the problem. Qt Creator has several problems that make this non-intuitive. Firstly if you right click in the object list on a Page, you don't get the layout sub-menu at all -- why not? To actually add a layout to a page you have to right click on the stack object with correct page set, this will add a layout to the page, but everything in this context menu (promote, change name etc) indicates that the object of interest is the parent stack... grrrr.... Ian Nov 7, 2012 at 0:37 Thanks, I found out through your answer that adding a layout to contain the QStackedWidget is also mandatory if you work only with C++/Qt (without Qt Designer). user6547518 May 13, 2021 at 20:42

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question . Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers .