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();
}