且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么QScrollArea的大小受到限制?

更新时间:2023-11-26 14:23:22

问题是,我希望scrollArea占据整个 窗口,但不是.调整窗口大小时,它也不会调整大小.

The problem is, that I want the scrollArea to occupy the entire window, but it does not. It also doesn't get resized when I resize the window.

原因是您尚未设置任何布局来管理QScrollArea小部件本身的位置,因此它只是留给自己的设备使用(因此,它只选择默认的大小和位置)本身并保持在该大小和位置).

The reason is that you have not set any kind of layout to manage the positioning of your QScrollArea widget itself, so it is just being left to its own devices (and therefore it just chooses a default size-and-location for itself and stays at that size-and-location).

一个简单的解决方法是将这些行添加到MainWindow构造函数的底部:

A simple fix would be to add these lines to the bottom of your MainWindow constructor:

QBoxLayout * mainLayout = new QVBoxLayout(this);
mainLayout->setMargin(0);
mainLayout->addWidget(scrollArea);