背景:让滚动区域中的内容的左右随着窗口自适应,上下内容超出屏幕,出现滚动。
方法:
widget = new QWidget;
sWidget = new QScrollArea;
sWidget->setWidget(widget);
//sWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
sWidget->setWidgetResizable(true);
关键函数在于setWidgetResizable:
This property holds whether the scroll area should resize the view widget
If this property is set to false (the default), the scroll area honors the size of its widget. Regardless of this property, you can programmatically resize the widget using widget()->resize(), and the scroll area will automatically adjust itself to the new size.
If this property is set to true, the scroll area will automatically resize the widget in order to avoid scroll bars where they can be avoided, or to take advantage of extra space.
此属性保存滚动区域是否应调整视图小部件的大小
如果将此属性设置为false(默认值),则滚动区域将遵循其小部件的大小。 无论此属性如何,都可以使用widget()-> resize()以编程方式调整窗口小部件的大小,并且滚动区域将自动将其自身调整为新的大小。
如果将此属性设置为true,则滚动区域将自动调整窗口小部件的大小,以避免在可以避免滚动条的地方使用滚动条,或者利用多余的空间。