我有一个 QQuickWidget。我想根据其 QML 内容使其可自动调整,并提供水平和垂直滚动条,以防其内容无法适应窗口。
我将 QQuickWidget 放在 QScrollArea 内,并向 QScrollArea 添加布局以使 QQuickWidget 填充它。在父窗口构造函数中,我添加了一行:
scroll_area->setWidget(quick_widget);
但是没有滚动条可用,与 QML 内容大小无关。QQuickWidget 和 QScrollArea 应该如何配置为我需要的工作?
UPDATE1:嗯,我正在尝试通过 QDesigner 来实现。实际的 UI XML 是:
<widget class="QScrollArea" name="scroll_area">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QQuickWidget" name="quick_widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>502</width>
<height>269</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="resizeMode">
<enum>QQuickWidget::SizeViewToRootObject</enum>
</property>
<property name="source">
<url>
<string>…</string>
</url>
</property>
</widget>
</widget>
在调试时,我可以看到 QScrollArea.widget() 已经设置为 quick_widget(没有强制调用 scroll_area.setWidget()),但仍然没有可用的滚动条,即使 quick_widget 内容不适合它的大小。
UPDATE2:那么动态添加项目可能存在问题。也许应该手动修改根对象的高度和宽度(或者不应该?)例如:
if ( object.x + object.width > root.width )
root.width = object.x + object.width;
if ( object.y + object.height > root.height )
root.height = object.y + object.height;