C++ Qt窗口的固定纵横比
我读过很多关于这个话题的问题,但我发现要么没有答案,要么不做我想做的,要么就是根本不工作 我的问题是在调整主窗口大小时保持高宽比。到目前为止,我已经看到了覆盖“QLayout”并使用“heightForWidth”执行契约、使用“resizeEvent”回调更改大小提示以及使用“resizeEvent”回调调整窗口大小的建议 我提到的前两个选项仅适用于拖动宽度的示例;我希望用户能够拖动宽度或高度。这两个函数中的第二个我试图根据我的目的进行调整,但它导致我的代码崩溃。第一个依赖于'heightForWidth'函数,我找不到'widthForHeight'函数{此线程中的注释支持我的结论:}。第三个选项是我最初尝试的,但有很多错误:大多数情况下,窗口会快速恢复到其原始大小,并在使用鼠标拖动边框时快速转换到它应该位于的位置 导致崩溃的尝试:C++ Qt窗口的固定纵横比,c++,windows,qt,C++,Windows,Qt,我读过很多关于这个话题的问题,但我发现要么没有答案,要么不做我想做的,要么就是根本不工作 我的问题是在调整主窗口大小时保持高宽比。到目前为止,我已经看到了覆盖“QLayout”并使用“heightForWidth”执行契约、使用“resizeEvent”回调更改大小提示以及使用“resizeEvent”回调调整窗口大小的建议 我提到的前两个选项仅适用于拖动宽度的示例;我希望用户能够拖动宽度或高度。这两个函数中的第二个我试图根据我的目的进行调整,但它导致我的代码崩溃。第一个依赖于'heightFo
void window::resizeEvent(QResizeEvent *event)
{
//window::resizeEvent(event); causes crash
if ((this->width() * 5 / 8) > (this->height() + 1)
|| (this->width() * 5 / 8) < (this->height() - 1))
{
updateGeometry();
}
}
QSize window::sizeHint() const
{
QSize s = size();
if (lastWidth != s.width())
{
s.setHeight((s.width()*5)/8);
//s.setWidth(window::sizeHint().width());
}
else if (lastHeight != s.height())
{
s.setWidth((s.height()*8)/5);
//s.setHeight(window::sizeHint().height());
}
return s;
}
有错误的尝试:
void window::resizeEvent(QResizeEvent *)
{
//If the window does not have the correct aspect ratio
//This should be false if this function caused the resize event,
// preventing a loop
if ((this->width() * 5 / 8) > (this->height() + 1)
|| (this->width() * 5 / 8) < (this->height() - 1))
{
int currentHeight = this->height();
int currentWidth = this->width();
//Change the dimension the user did not to match the user's change.
if (lastWidth != currentWidth)
{
lastWidth = currentWidth;
currentHeight = currentWidth * 5/8;
lastHeight = currentHeight;
}
else
{
lastHeight = currentHeight;
currentWidth = currentHeight * 8/5;
lastWidth = currentWidth;
}
//update the change
this->resize(currentWidth,currentHeight);
}
}
它实际上是Qt,不是Qt-谢谢^。^您有没有可能知道如何解决此问题?对不起,没有可能:-