简体   繁体   English

为升级的小部件设置父级QWidget

[英]Set parent QWidget for promoted widgets

I have a Main QWidget which contains three promoted widgets: A derived from QGraphicsView , B derived from QLabel and C derived from QWidget . 我有一个Main QWidget ,其中包含三个已升级的窗口小部件: AQGraphicsView派生, BQLabel派生和CQWidget派生。
On application exit I need B and C to be destroyed before A , but I figured out that since A , B and C parent is automatically set to Main , A is destroyed first. 在应用程序退出时,我需要在A之前销毁BC ,但是我发现由于ABC parent会自动设置为Main ,因此A首先被销毁。 This can be solved setting A as B and C parent. 可以通过将A设置为BC父级来解决。

The generated ui_ProjectName.h file has: 生成的ui_ProjectName.h文件具有:

a = new A(Main);
b = new B(Main);
c = new C(Main);

But it should be: 但是应该是:

a = new A(Main);
b = new B(a);
c = new C(a);

Is there a way to do this from Qt Designer directly to generate the correct ui_ProjectName.h ? 有没有一种方法可以从Qt Designer直接生成正确的ui_ProjectName.h

If your 3 widgets do not belong to same layout you can manually edit .ui file to assign proper parent to a widget. 如果您的3个窗口小部件不属于同一布局,则可以手动编辑.ui文件以将适当的父级分配给窗口小部件。 To make widget A a parent for a widget B you can just cut <widget> tag that belongs to B and paste it inside A 's <widget> tag. 为了让小部件A一个小窗口父B ,你可以只是削减<widget>属于标记B ,并粘贴里面 A<widget>标签。 Like this: 像这样:

<widget class="QWidget" name="A">
    <widget class="QWidget" name="B">
       ...
    </widget>
</widget>

Note that QtCreator has a legal way to edit .ui files: click right mouse button on .ui file and then choose "Open With -> Plain Text Editor". 请注意,QtCreator具有编辑.ui文件的合法方法:在.ui文件上单击鼠标右键,然后选择“打开方式->纯文本编辑器”。

But pay attention : this will break layout rules if you have them. 但是请注意 :如果有布局规则,则会违反布局规则。 If you do not want to break layout for this case you probably can try reassign parents in runtime via code: 如果您不想在这种情况下中断布局,则可以尝试在运行时通过代码重新分配父级:

bWidget->setParent(aWidget);
cWidget->setParent(aWidget);

In this case it doesn't actually matter what code was generated in ui_*.h file — you will reset it. 在这种情况下,在ui_*.h文件中生成什么代码实际上并不重要-您将重置它。

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

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