-1

I have a QQuickWidget. I'd like to make it autosizable according to its QML content and to provide both horizontal and vertical scrollbars in case when its content cannot fit the window.

I put the QQuickWidget inside a QScrollArea and added layout to the QScrollArea to make the QQuickWidget to fill it. In the parent window constructor I added a line:

scroll_area->setWidget(quick_widget);

However no scroll bars are available, independently on QML content size. How should QQuickWidget and QScrollArea be configured to work as I need?

UPDATE1: Well, I'm trying to make that via QDesigner. Actual UI XML is:

<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>

And while debugging I can see that QScrollArea.widget() has been already set to quick_widget (without force call scroll_area.setWidget()), but still there are no scrollbars available, even when the quick_widget content cannot fit its size.

UPDATE2: Well there's possibly a problem that items are added dynamically. And perhaps the root object height and width should be modified manually (or they shouldn't?) For example:

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;
Vercetti
  • 437
  • 1
  • 6
  • 17

2 Answers2

1

I have tried following code, and it works fine.

//mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    auto scrollArea = new QScrollArea();
    setCentralWidget(scrollArea);

    auto quickWidget = new QQuickWidget();
    quickWidget->setSource(QUrl("qrc:/main.qml"));
    quickWidget->setResizeMode(QQuickWidget::SizeViewToRootObject);
    scrollArea->setWidget(quickWidget);
}
//main.qml
//a varying Rectangle
import QtQuick 2.0

Rectangle {
    id: root ;color: "red"; width: 400; height: 500
    Timer{
        id: timer
        property real time
        interval: 400
        repeat: true
        onTriggered: {
            root.width = 800 + Math.sin(time*100) *100
            time = time + 1;
            console.log(time)
        }
    }

    Component.onCompleted: {
        timer.time = 0
        timer.start()
    }
}

When the root retangle is small

When the root retangle is big

Zen
  • 5,065
  • 8
  • 29
  • 49
0

You have to set the widgetResizable property of the QScrollArea to false (be default is true).

You might also wanna set the Horizontal and Vertical alignments to AlignLeft and AlignTop respectivelly.

See the docs for widgetResizable property