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

GridView and ObjectModel. Highlight size and resize of dynamically added elements.



  • Hi. I have three questions about GridView and ObjectModel.
    Here is a little example to explain my problems:

    main.qml

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import QtQuick.Controls 2.12
    import QtQml.Models 2.12
    import QtQuick.Layouts 1.12
    
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
        Page
        {
            width: parent.width
            height: parent.height
            footer: Button{
                width: parent.width
                height:  100
                text: "test"
                function createObject()
                {
    
                    var component = Qt.createComponent("CameraPreview.qml");
                    if (component.status == Component.Ready)
                    {
                        var newObject = component.createObject(om, {"width" : test.cellWidth - 10, 
    "height" : test.cellHeight - 10});
                        om.append(newObject)
                    }
                }
                onClicked: createObject()
            }
            Pane
            {
                anchors.top: parent.top + 100
                width: parent.width
                height: parent.height
                id: syncTab
                GridView {
                    id: test
                    anchors.fill: parent
                    cellWidth: width / 4
                    cellHeight: height / 3
                    highlight: Rectangle { width : test.cellWidth; height: test.cellHeight; 
    color: "lightsteelblue"; radius: 5; }
                    focus: true
                    model: ObjectModel {
                        id: om
                        CameraPreview {width : test.cellWidth - 30
                            height : test.cellHeight - 30 }
                        CameraPreview {width : 10
                            height : 10 }
    
                    }
                }
    
            }
        }
    }
    

    and CameraPreview.qml

    import QtQuick 2.0
    
    Rectangle
    {
        color: "black"
        opacity: 0.3
    }
    

    First question: Why is highlight size is same as element size? I set highlight rectangle size as :

    width : test.cellWidth; height: test.cellHeight;
    

    and first element size is:

    width : test.cellWidth - 30 height : test.cellHeight - 30
    

    but highlight covers just element area, not all cell area

    Second question:
    Why dynamically added objects don't resize when i resize GridView?

    var newObject = component.createObject(om, {"width" : test.cellWidth - 10, 
    "height" : test.cellHeight - 10});
    

    here new element size depends on GridView size, but it doesn't work when i resize window.

    And my last question is how to change currentIndex by mouse click?


Log in to reply