简体   繁体   English

在 Qt 的两种不同布局中使用相同的小部件

[英]Use the same widget in two different layouts in Qt

I would like to use the same widget in two different layouts in Qt.我想在 Qt 的两个不同布局中使用相同的小部件。 Here is my code:这是我的代码:

QWidget *myWidget = new QWidget;

QFormLayout *layout1 = new QFormLayout;
layout1->addWidget(myWidget);

QFormLayout *layout2 = new QFormLayout;
layout2->addWidget(myWidget);

The widget is as it should in layout2 but is not visible in layout1 .小部件在layout2应该是layout2但在layout1不可见。

A workaround would be to create two different myWidget widgets, but I would like to know if there is a better way of doing.一种解决方法是创建两个不同的myWidget小部件,但我想知道是否有更好的方法。

Why does this happen and what is the correct way of doing this?为什么会发生这种情况,这样做的正确方法是什么?

addWidget transfers the ownership from layout1 to layout2 . addWidget将所有权从layout1转移到layout2
Object trees are the way Qt uses to organize objects.对象树是 Qt 用来组织对象的方式。 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.您可以尝试解决此限制,但这不是您应该使用 Qt 的方式,我不建议这样做。
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 对象模型的更多详细信息,请参见此处

You cannot have the same object in multiple places.您不能在多个地方拥有相同的对象。 There is only once instance of it and it lives in only one single location.它只有一次实例并且它只存在于一个位置。 You can only have multiple references.您只能有多个引用。 Granted, a layout doesn't take a widget instance, but a reference (pointer) to it, but Qt's design is such that adding a widget to a layout will transfer ownership to the layout's underlying widget.当然,布局不接受小部件实例,而是对其的引用(指针),但 Qt 的设计是这样的,将小部件添加到布局会将所有权转移给布局的底层小部件。 And it makes sense, the two layouts may call for a different widget geometry, and how does a single widget have two geometries in the same time?这是有道理的,两种布局可能需要不同的小部件几何形状,单个小部件如何同时具有两个几何形状? Even if possible theoretically, it is not possible without abstracting the geometry away from the widget, and in Qt's case the geometry is part of the widget, so it is not possible.即使理论上可能,如果不将几何图形从小部件中抽象出来也是不可能的,在 Qt 的情况下,几何图形是小部件的一部分,所以这是不可能的。 And that's just one of the many aspects which make such reuse/sharing challenging and not really viable.这只是使这种重用/共享具有挑战性且不可行的众多方面之一。

Depending on what you want to achieve you could:根据您想要实现的目标,您可以:

  • reuse GUI elements - in that case roll out YourOwnWidget : public QWidget , then you can instantiate in as many times as you want重用 GUI 元素 - 在这种情况下推出YourOwnWidget : public QWidget ,然后您可以根据需要多次实例化

  • share the same data across multiple GUI elements, in addition to the previous step, what you really want to do is put the data in a separate, non-visible object, then you can create and bind as many types and instances of GUI elements to it as you want.跨多个 GUI 元素共享相同的数据,除了上一步之外,您真正想做的是将数据放在一个单独的、不可见的对象中,然后您可以创建和绑定尽可能多的 GUI 元素类型和实例到随心所欲。

You can use QGraphicsView .您可以使用QGraphicsView Define one instance of QGraphicsView and add it to layout1 :定义一个QGraphicsView实例并将其添加到layout1

QGraphicsView *gv1 = new QGraphicsView();
layout1->addWidget(gv1);

Define another one and add it to layout2 :定义另一个并将其添加到layout2

QGraphicsView *gv2 = new QGraphicsView();
layout2->addWidget(gv2);

QGraphicsScene *qc = new QGraphicsScene();
qc->addWidget(myWidget);

Now set scene to your QGraphicsView objects现在将场景设置为QGraphicsView对象

gv1->setScene(qc);
gv2->setScene(qc);

After that you have two views containing the same widget.之后,您有两个包含相同小部件的视图。

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

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