如何在调整 window 大小时锁定纵横比?
How to lock aspect ratio while resizing the window?
我有一个 qt 应用程序可以打开 window 来显示图像。我希望能够调整 window 的大小并保持 window 的纵横比。 window 的原始尺寸为 480x640(宽 x 高)。我想要的效果是,当我改变宽度时,我会随之改变高度,当我改变高度时,我会随之改变宽度。如何检测我是否正在更改 window.
的宽度或高度
我实现了下面的代码,当我改变宽度时它确实保持了纵横比,但是我无法改变 window 的高度。我不能让高度变小或变大。
下面的代码创建 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 */
下面的代码是主要功能。
#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();
}
How to lock the aspect ratio while resizing the window?
在这种情况下,我们可以利用 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.S。在实际尝试调试之后,我已经用 if
运算符更正了这个解决方案。我们是否应该通过拖动小部件的一角来调整小部件的大小,然后宽度优先(需要选择一个,否则它还能如何工作?)
我有一个 qt 应用程序可以打开 window 来显示图像。我希望能够调整 window 的大小并保持 window 的纵横比。 window 的原始尺寸为 480x640(宽 x 高)。我想要的效果是,当我改变宽度时,我会随之改变高度,当我改变高度时,我会随之改变宽度。如何检测我是否正在更改 window.
的宽度或高度我实现了下面的代码,当我改变宽度时它确实保持了纵横比,但是我无法改变 window 的高度。我不能让高度变小或变大。
下面的代码创建 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 */
下面的代码是主要功能。
#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();
}
How to lock the aspect ratio while resizing the window?
在这种情况下,我们可以利用 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.S。在实际尝试调试之后,我已经用 if
运算符更正了这个解决方案。我们是否应该通过拖动小部件的一角来调整小部件的大小,然后宽度优先(需要选择一个,否则它还能如何工作?)