简体   繁体   中英

Dynamically add a custom widget to a baked-in layout

I'm working on some desktop software that requires one static window with a frame filled with dynamic content. The frame is on the main window, and the main window has a grid layout that lets the widgets I've added through the editor to automatically resize/expand with window resizes.

I have created a custom widget (and made some variations with a frame and dockable widget) to try and get the desired behaviour. When I add this widget to a new layout (I've tried both QVBoxLayout and QGridLayout) and tell the frame to use that layout, no widget gets displayed, however, if I create the frame variant of the widget and parent it with the frame in the constructor (aka new CustomWidget( this->ui->my_frame ); ), it displays, but is clearly not following the layout (does not expand). No other combination of my custom widget (as a QWidget, QFrame, or QDockableWidget) will display in my frame, and if in the off-chance it does, it does not expand with window resize events.

I have read in some places that overriding the main window's resize event function may resolve this but that is only in cases when I am not using a layout - I'd much prefer using a layout.

I can get the desired output if I use fixed content and the QTabWidget but the client I am building this application for would greatly prefer me not to use tabs. Their competitor's software uses tabs and we both agree that it looks clunky.

I'm going to end up code the widget interface, I think, but now I'm curious why importing these widgets into a layout doesn't work as expected.

If I understand your description correctly, you have a main window with a blank QFrame that you wish to use to dynamically add other QWidgets to at runtime.

When adding new child QWidgets to the place-holder QFrame, you must add them to the QLayout of the placeholder QFrame, not the main window, if you wish them to be automatically placed and resized.

I would suggest trying to start with a subclass of a simple widget (QLabel with some text for example) and use that in your code to see if it works. It maybe that you need to add code to handle what Qt expects the widget to do and that is being handled already by the existing widgets.

From past experience what you describe (add to layout, assign that layout to the frame) works and is the right way of doing it. So I imagine there must be some code that handles the layout signals missing from your custom widgets.

For example:

Layouts are an elegant and flexible way to automatically arrange child widgets within their container. Each widget reports its size requirements to the layout through the sizeHint and sizePolicy properties, and the layout distributes the available space accordingly.

From: Widgets and Layouts doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM