1
votes

I have a qt application that opens up a window that will display an image. I want to able to resize the window and maintain the aspect ratio of the window. The original size of the window is 480x640 (width x height). I want the effect that when I change the width, I change the height along with it and when I change the height, I change the width with it. How can I detect if I am changing the width or the height of the window.

I implemented the code below and it does maintain the aspect ratio when I change the width, but however I am not able to change the height of the window. I can't make the height smaller or bigger.

The code below creates the window.

#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 */

The code below is the main function.

#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
if you want to detect whether you are changing the width or height, its easy. When you change the width, the previous height remains and vice versa. So check if the new height is the same as the original height you are changing the width. - gallickgunner
Some stuff under this old Q of mine might be of interest: stackoverflow.com/questions/40419768/… - hyde

1 Answers

1
votes

How to lock the aspect ratio while resizing the window?

In this case we can take an advantage of overloading of 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
}

Mind that no sizing hints applied for this solution (default).

P.S. I've corrected this solution with if operator after actually trying to debug it. Should we resize the widget by dragging the corner of it then the width is priority (need to choose one, how else can be that working anyway?)