调整主窗口大小时强制使用纵横比

Jas*_*enX 5 qt

我知道在 Qt4 中,您无法设置单个标志来让鼠标驱动的窗口大小调整保持一定的纵横比(例如 1:1)。有没有办法在“resizeEvent (QResizeEvent * event)”中强制窗口的新大小?事件的参数似乎不支持更改或给用户注入新大小的机会。

我的目标:GUI/鼠标调整某个窗口的大小将保持其当前的纵横比,无论您从哪里调整它的大小(侧面、顶部、底部、角落)。

blu*_*kin 2

重复的问题? 如何在 Qt 中保持小部件的长宽比?

引用上面链接中“df”的答案

从 resizeEvent() 中调用 resize() 对我来说从来都没有很好的效果——充其量它会导致窗口大小调整两次时闪烁(就像你一样),最坏的情况是无限循环。

我认为保持固定宽高比的“正确”方法是创建自定义布局。您只需重写两个方法:QLayoutItem::hasHeightForWidth() 和 QLayoutItem::heightForWidth()。

另请查看 Sam Dutton 的解决方案@ http://lists.trolltech.com/qt-interest/2007-01/msg00204.html

void MyWindow::resizeEvent(QResizeEvent * /*resizeEvent*/) 
{
    int containerWidth = _myContainerWidget->width();
    int containerHeight = _myContainerWidget->height();

    int contentsHeight = containerHeight ;
    int contentsWidth = containerHeight * _aspectRatio;
    if (contentsWidth > containerWidth ) {
        contentsWidth = containerWidth ;
        contentsHeight = containerWidth / _aspectRatio;
    }

    resizeContents(contentsWidth, contentsHeight);
}
Run Code Online (Sandbox Code Playgroud)

  • 不知道为什么我必须处理布局?我只需要主窗框保持纵横比。内部小部件与任何尺寸的主框架兼容,因为当我自由调整框架大小时它们可以很好地拉伸 (6认同)