如何在调整窗口大小时锁定纵横比?

Posted

技术标签:

【中文标题】如何在调整窗口大小时锁定纵横比?【英文标题】:How to lock aspect ratio while resizing the window? 【发布时间】:2018-06-11 03:01:40 【问题描述】:

我有一个 qt 应用程序,它会打开一个显示图像的窗口。我希望能够调整窗口大小并保持窗口的纵横比。窗口的原始大小为 480x640(宽 x 高)。我想要这样的效果,当我改变宽度时,我会随之改变高度,当我改变高度时,我会随之改变宽度。如何检测我是否正在更改窗口的宽度或高度。

我实现了下面的代码,当我改变宽度时它确实保持纵横比,但是我无法改变窗口的高度。我不能让高度变小或变大。

下面的代码创建了窗口。

#ifndef VideoWidget_h
#define VideoWidget_h

#include <iostream>

// Qt libraries
#include <QApplication>
#include <QtCore>
#include <QWidget>
#include <QGLWidget>

using namespace cv;
using namespace std;

class VideoWidget : public QGLWidget

    Q_OBJECT

public:
    VideoWidget()
    

    

protected:

    void resizeGL(int width, int height)
    
        const float ASPECT_RATIO = (float) WIDTH / (float) HEIGHT;
        const float aspect_ratio = (float) width / (float) height;

        if (aspect_ratio > ASPECT_RATIO) 
            height = (1.f / ASPECT_RATIO) * width;
            resize(width, height);
         else if (aspect_ratio < ASPECT_RATIO) 
            height = ASPECT_RATIO * height;
            resize(width, height);
        
    

private:
    int WIDTH = 480;
    int HEIGHT = 640;

;


#endif /* VideoWidget_h */

下面的代码是main函数。

#include <iostream>

// Qt libraries
#include <QApplication>
#include <QtCore>
#include <QWidget>
#include <QGLWidget>

#include "VideoWidget.h"

using namespace cv;
using namespace std;

int main(int argc, char **argv)

    QApplication app (argc, argv);

    VideoWidget v;
    v.resize(480, 640);
    v.show();

    return app.exec();

【问题讨论】:

如果你想检测你是在改变宽度还是高度,这很容易。当您更改宽度时,之前的高度保持不变,反之亦然。因此,请检查新高度是否与您要更改宽度的原始高度相同。 我这个老 Q 下的一些东西可能会感兴趣:***.com/questions/40419768/… 【参考方案1】:

如何在调整窗口大小时锁定纵横比?

在这种情况下,我们可以利用 QWidget::resizeEvent: 的重载:

void MyWidget::resizeEvent(QResizeEvent *event)

   static const float ratioY2X = (float) HEIGHT / (float) WIDTH;

   // QResizeEvent type has oldSize() and size()
   // and size() has an actual new size for the widget
   // so we can just take it and apply the ratio.

   // first try to detect the direction of the widget resize
   if (event->oldSize().width() != event->size().width())
      this->resize(event->size().width(),             // apply the correct ratio
                   event->size().width() * ratioY2X); // to either of width/height
   else
      this->resize(event->size().height() / ratioY2X, // apply the correct ratio
                   event->size().height());           // to either of width/height
   event->accept(); // we've finished processing resize event here

请注意,此解决方案没有应用尺寸提示(默认)。

附:在实际尝试调试后,我已使用 if 运算符更正了此解决方案。我们是否应该通过拖动它的角来调整小部件的大小,然后宽度是优先的(需要选择一个,否则它怎么能工作?)

【讨论】:

出于好奇,那段代码不会产生无限循环吗? 我可能有几年没有这样做了,但确实奏效了。如果确实需要,我们可以区分要处理的事件源:doc.qt.io/qt-5/qevent.html#spontaneous 因此,如果是,则按“自发”处理。对于这种情况,Qt 知道它不是来自实际事件的事件循环。 这种方法是成功的,需要更好的逻辑。 知道为什么我在尝试实现此功能时会收到“成员访问不完整类型 'QResizeEvent'”的错误吗? #include &lt;QResizeEvent&gt;

以上是关于如何在调整窗口大小时锁定纵横比?的主要内容,如果未能解决你的问题,请参考以下文章

WPF的窗口中的所有内容随窗口大小变化而同步变化

如何使用 Cordova/Ionic 将图像裁剪为特定纵横比?

当固定纵横比 QWidget 无法填满整个窗口时创建填充

C#问题 锁定WinForm窗口大小

OPENGL如何在改变窗口大小时,使自己的图形不被拉伸保持自己的纵横比,把原理也解释下,不要只给代码

根据窗口大小和纵横比缩放图像