在Qt中以两种不同的布局使用相同的小部件 [英] Use the same widget in two different layouts in Qt
问题描述
我想在Qt中的两个不同布局中使用相同的小部件.这是我的代码:
I would like to use the same widget in two different layouts in Qt. Here is my code:
QWidget *myWidget = new QWidget;
QFormLayout *layout1 = new QFormLayout;
layout1->addWidget(myWidget);
QFormLayout *layout2 = new QFormLayout;
layout2->addWidget(myWidget);
该小部件在layout2
中应有的状态,但在layout1
中不可见.
The widget is as it should in layout2
but is not visible in layout1
.
一种解决方法是创建两个不同的myWidget
小部件,但我想知道是否有更好的方法.
A workaround would be to create two different myWidget
widgets, but I would like to know if there is a better way of doing.
为什么会发生这种情况,正确的方法是什么?
Why does this happen and what is the correct way of doing this?
推荐答案
addWidget
将所有权从layout1
转移到layout2
.
对象树是Qt用于组织对象的方式.例如,具有父对象的项目显示在其父对象的坐标系中,并通过其父对象的边界以图形方式进行裁剪.
您可以尝试解决此限制,但这不是您应该使用Qt的方式,我不建议这样做.
如果需要两个窗口小部件,请创建两个窗口小部件.这就是Qt的设计方式和使用方式.
addWidget
transfers the ownership from layout1
to layout2
.
Object trees are the way Qt uses to organize objects. As an example, an item that has a parent is displayed in its parent's coordinate system and is graphically clipped by its parent's boundaries.
You can try to work around the limitation, but it is not how you should use Qt and I won't suggest it.
If you need two widgets, create two widgets. That's how Qt is designed and how it should be used.
有关Qt对象模型的更多详细信息,请参见此处.
See here for further details about the Qt's objects model.
这篇关于在Qt中以两种不同的布局使用相同的小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!