简体   繁体   English

动态地将自定义窗口小部件添加到嵌入式布局

[英]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). 当我将此小部件添加到新布局(我尝试过QVBoxLayout和QGridLayout)并告诉框架使用该布局时,不会显示任何小部件,但是,如果我创建了小部件的框架变体并将其作为框架的父项在构造函数中(也称为new CustomWidget(this-> ui-> my_frame);),它会显示,但显然不遵循布局(不会扩展)。 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. 我的自定义窗口小部件的其他组合(如QWidget,QFrame或QDockableWidget)将不会显示在我的框架中,并且如果不在此范围内,它将不会随着窗口调整大小事件而扩展。

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. 我在某些地方已经读到,重写主窗口的resize事件功能可以解决此问题,但这仅在不使用布局的情况下-我更喜欢使用布局。

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. 如果我使用固定的内容和QTabWidget,则可以得到所需的输出,但是要为其构建此应用程序的客户端将更希望我不要使用选项卡。 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. 如果我正确理解了您的描述,则您会在主窗口中看到一个空白的QFrame,希望在运行时将其动态添加到其他QWidget。

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. 将新的子QWidget添加到占位符QFrame时,如果希望自动放置它们并调整其大小,则必须将它们添加到占位符QFrame而不是主窗口的QLayout中。

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. 我建议尝试从一个简单的小部件(例如QLabel带有一些文本)的子类开始,并在您的代码中使用它来查看它是否有效。 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. 可能您需要添加代码来处理Qt期望小部件执行的操作,并且该代码已由现有小部件处理。

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. 每个小部件都通过sizeHint和sizePolicy属性向布局报告其大小要求,并且布局会相应地分配可用空间。

From: Widgets and Layouts doc 来自: 小部件和布局文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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