Qt:两个不同布局中的相同小部件

时间:2021-03-17 12:14:58

What I'm trying to achieve is that a widget could exist in two different layouts, in this case in QHBoxLayout and in QVBoxLayout. I'm implementing a system which dynamically switches between the two layouts when a device's screen orientation changes.

我想要实现的是一个小部件可以存在于两个不同的布局中,在本例中是QHBoxLayout和QVBoxLayout。我正在实现一个系统,当设备的屏幕方向改变时,该系统在两个布局之间动态切换。

Currently I'm creating, let's say a complex composite widget called MyWidget and adding it into a two different layouts:

目前我正在创建,假设一个名为MyWidget的复杂复合窗口小部件并将其添加到两个不同的布局中:

MyWidget *wgt = new QWidget();
QVBoxLayout vlayout;
QHBoxLayout hlayout;

vlayout->addWidget(wgt);
hlayout->addWidget(wgt);

Now imagine that both layouts are hosted within a 'root' layout, and that this root layout can resize into a more wide than tall 'landscape' mode, and into a more tall than wide 'portrait' mode.

现在想象两个布局都在“根”布局中托管,并且这个根布局可以调整为更宽的高度“横向”模式,并且可以调整为更高而不是宽的“纵向”模式。

The MyWidget shows correctly in only the first layout it is added into, and when the layouts are switched, it shows all wrong or not at all.

MyWidget仅在添加到的第一个布局中正确显示,并且当布局切换时,它显示所有错误或根本不显示。

I don't know if I'm making any sense here, but this is my problem. Maybe when the switch event is called all child widgets and layouts should be resized, so it would always look right. Only problem is that I don't know how.

我不知道我在这里是否有任何意义,但这是我的问题。也许当调用switch事件时,应调整所有子窗口小部件和布局的大小,因此它总是看起来正确。唯一的问题是我不知道如何。

2 个解决方案

#1


4  

This isn't particularly easy to do, but is possible.

这不是特别容易,但是可以。

First of all, I'd recommend that you actually create two different widgets, one for the vertical and one for the horizontal, and manage things that way. If the source data is properly separated from the UI class, you should be able to do so without too much trouble, but by incurring some memory overhead.

首先,我建议你实际创建两个不同的小部件,一个用于垂直,一个用于水平,并以这种方式管理。如果源数据与UI类正确分离,您应该能够毫不费力地这样做,但会产生一些内存开销。

One way to do as you desire would be to completely remove the widgets from one layout and add them to the other when you need to change the arrangement on the screen, and change the layout that is added to the widget. This should cause the same widgets to be drawn in a different way.

您需要的一种方法是从一个布局中完全删除窗口小部件,并在需要更改屏幕上的排列时将它们添加到另一个布局,并更改添加到窗口小部件的布局。这应该导致以不同的方式绘制相同的小部件。

A different, more intricate way of handling this (although potentially more efficient) would be to write your own layout and have it handle rearranging widgets based on the orientation change.

一种不同的,更复杂的处理方式(尽管可能更有效)是编写自己的布局并让它根据方向变化处理重新排列小部件。

#2


12  

This isn't a general solution for changing layouts, but an easy solution in your case: Just change the boxlayout's direction.

这不是改变布局的一般解决方案,而是一种简单的解决方案:只需更改boxlayout的方向即可。

hlayout->setDirection(QBoxLayout::TopToBottom);
// now your hboxlayout works as vertical layout

hlayout->setDirection(QBoxLayout::LeftToRight);
// and now it is horizontal again

#1


4  

This isn't particularly easy to do, but is possible.

这不是特别容易,但是可以。

First of all, I'd recommend that you actually create two different widgets, one for the vertical and one for the horizontal, and manage things that way. If the source data is properly separated from the UI class, you should be able to do so without too much trouble, but by incurring some memory overhead.

首先,我建议你实际创建两个不同的小部件,一个用于垂直,一个用于水平,并以这种方式管理。如果源数据与UI类正确分离,您应该能够毫不费力地这样做,但会产生一些内存开销。

One way to do as you desire would be to completely remove the widgets from one layout and add them to the other when you need to change the arrangement on the screen, and change the layout that is added to the widget. This should cause the same widgets to be drawn in a different way.

您需要的一种方法是从一个布局中完全删除窗口小部件,并在需要更改屏幕上的排列时将它们添加到另一个布局,并更改添加到窗口小部件的布局。这应该导致以不同的方式绘制相同的小部件。

A different, more intricate way of handling this (although potentially more efficient) would be to write your own layout and have it handle rearranging widgets based on the orientation change.

一种不同的,更复杂的处理方式(尽管可能更有效)是编写自己的布局并让它根据方向变化处理重新排列小部件。

#2


12  

This isn't a general solution for changing layouts, but an easy solution in your case: Just change the boxlayout's direction.

这不是改变布局的一般解决方案,而是一种简单的解决方案:只需更改boxlayout的方向即可。

hlayout->setDirection(QBoxLayout::TopToBottom);
// now your hboxlayout works as vertical layout

hlayout->setDirection(QBoxLayout::LeftToRight);
// and now it is horizontal again