问题描述
我正在创建一个新的小部件,通过继承QWidget类。我想为这个小部件设置一个比例(对于它的高度和宽度),并且总是保持不变。
I'm creating a new widget, by subclassing the QWidget class. I'd like to be able to set a ratio (for its height and its width) for this widget, which will always be maintained.
为此,总是搜索,使用Qt5文档,Google和Stackoverflow。显然,我找到了答案:特别是。但是,不幸的是,甚至没有一个完全有效:
For this, I've always searched, using the Qt5 documentation, Google, and Stackoverflow. Obviously, I've found answers: in particular, this one. But, unfortunately, not even one is fully effective:
- 设置
sizeIncrement
即使窗口小部件是一个窗口 - 我试图重载resizeEvent,但我真的不知道该怎么做...
-
如果我遵循,有两件事:
- Setting the
sizeIncrement
does totally nothing, even if the widget is a window - I tried to overload resizeEvent, but I really don't know how to do this...
If I follow this answer, two things:
- 如果窗口小部件是顶级窗口,则比率不会维持,我可以根据需要调整大小。
- 如果我将此窗口小部件放置在布局中,如果我只是增加窗口的宽度和高度,则保持比率。但是,一旦我增加宽度或高度到很多,小部件是扁平。相反,我希望布局自动调整其大小以保持小部件的比率。
那么,我如何能够保持子类QWidget的宽高比?
So, how could I manage to keep the aspect ratio of a subclassed QWidget?
推荐答案
, AspectRatioWidget
),在其中放置您的小部件。对于父窗口部件,子类QWidget并给它一个QBoxLayout。把你的小部件放在中心,并在任一侧的QSpacerItems。然后在父窗口小部件的 QWidget :: resizeEvent
中根据需要调整方向和伸展。我提供了一个例子如下。要使用,只需创建 AspectRatioWidget
的实例,并传递构造函数指向您的小部件和所需的宽高比。
Create a parent widget (e.g., AspectRatioWidget
) in which to place your widget. For the parent widget, subclass QWidget and give it a QBoxLayout. Put your widget into the center, and QSpacerItems on either side. Then in the parent widget's QWidget::resizeEvent
adjust the direction and stretches as needed. I've provided an example below. To use, just create an instance of AspectRatioWidget
and pass the constructor a pointer to your widget and the desired aspect ratio.
// header
class AspectRatioWidget : public QWidget
{
public:
AspectRatioWidget(QWidget *widget, float width, float height, QWidget *parent = 0);
void resizeEvent(QResizeEvent *event);
private:
QBoxLayout *layout;
float arWidth; // aspect ratio width
float arHeight; // aspect ratio height
};
// cpp
AspectRatioWidget::AspectRatioWidget(QWidget *widget, float width, float height, QWidget *parent) :
QWidget(parent), arWidth(width), arHeight(height)
{
layout = new QBoxLayout(QBoxLayout::LeftToRight, this);
// add spacer, then your widget, then spacer
layout->addItem(new QSpacerItem(0, 0));
layout->addWidget(widget);
layout->addItem(new QSpacerItem(0, 0));
}
void AspectRatioWidget::resizeEvent(QResizeEvent *event)
{
float thisAspectRatio = (float)event->size().width() / event->size().height();
int widgetStretch, outerStretch;
if (thisAspectRatio > (arWidth/arHeight)) // too wide
{
layout->setDirection(QBoxLayout::LeftToRight);
widgetStretch = height() * (arWidth/arHeight); // i.e., my width
outerStretch = (width() - widgetStretch) / 2 + 0.5;
}
else // too tall
{
layout->setDirection(QBoxLayout::TopToBottom);
widgetStretch = width() * (arHeight/arHeight); // i.e., my height
outerStretch = (height() - widgetStretch) / 2 + 0.5;
}
layout->setStretch(0, outerStretch);
layout->setStretch(1, widgetStretch);
layout->setStretch(2, outerStretch);
}
这篇关于在调整大小时保持子类QWidget的宽高比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!