代码之家  ›  专栏  ›  技术社区  ›  Pete

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

  •  1
  • Pete  · 技术社区  · 7 年前

    我有一个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 */
    

    以下代码是主要功能。

    #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();
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Alexander V    7 年前

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

    在这种情况下,我们可以利用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
    }
    

    请注意,此解决方案没有应用大小调整提示(默认)。

    P、 我已经用 if 在实际尝试调试它之后。我们是否应该通过拖动小部件的一角来调整其大小,然后宽度是优先的(需要选择一个,否则该如何工作?)