Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Dealing with dynamic content in ScrollView



  • Hello!
    To describe my question I have created a following example:

    import QtQuick 2.15
    import QtQuick.Window 2.15
    import QtQuick.Layouts 1.12
    import QtQuick.Controls 2.12
    
    Window {
        width: 640
        height: 480
        visible: true
    
        ScrollView{
            id: container
            anchors.fill: parent
            contentWidth: width*2
            contentHeight: height*2
            property int dynamicElementCount: 0
            function sayHello(){
                console.log("Child element asked the container to say hello");
            }
            Button{
                text:"Create dynamic"
                onClicked: {
                    Qt.createQmlObject("
                        import QtQuick 2.15;
                        import QtQuick.Controls 2.12;
                        Button{
                            x: 100*container.dynamicElementCount;
                            text:\"Dynamic"+container.dynamicElementCount+"\";
                            onClicked:{ container.sayHello() }}
                    ", container.contentItem.contentItem, "Button"+container.dynamicElementCount);
                    container.dynamicElementCount++;
                }
            }
        }
    }
    

    I want to create dynamic element as ScrollView content, so it would be scrollable. As I've noticed, to scroll dynamic content its parent should be not the ScrollView itself but the QQuickItem that is 3rd element in ScrollView hierarchy: ScrollView contentItem is Flickable, and its contentItem is QQuickItem.
    So practically to create a dynamic element inside ScrollView I have to create it inside ScrollView.contentItem.contentItem. And if I want to address to ScrollView properties from the inside of dynamic object I could either use id or relative path will be parent.parent.propertyName.
    Is there a better way to do that?



  • Hello, you can always set a property in the parent object and bind it directly like:

    property1 = parent.propertyName

    property2 = child.propertyName


Log in to reply