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

How to use a GridLayout in combination with exposing data property



  • I am trying to wrap GridLayout inside an Item and exposing the GridLayout's data property as the default property of the item. But this results in two problems.

    1. I get a crash when exiting the application.

      • This may in fact be a bug in Qt itself and it might also already been fixed, if not I will report it after fixing 2. I am only able to test on Windows 7 using Qt 5.7.0 MSVC2015 32.bit at the moment.
    2. How to use the attached Layout property? Take look in the following example, which results in the error: "Non-existent attached object" on line "Layout.alignment: Qt.AlignBottom | Qt.AlignRight".

    //MyCustomLayout.qml
    import QtQuick 2.7
    import QtQuick.Layouts 1.3
    
    Item {
        default property alias data: layout.data
    
        //Some other QML components not to be within GridView here.
    
        GridLayout {
            id: layout
            anchors.fill: parent
        }
    
        //Some other QML components not to be within GridView here.
    
    }
    
    //main.qml
    import QtQuick.Controls 2.0
    
    ApplicationWindow {
        id: root
        visible: true
        height: 1024
        width: 768
    
        MyCustomLayout {
            anchors.fill: parent
    
            Button {
                Layout.alignment: Qt.AlignBottom | Qt.AlignRight
            }
        }
    }
    


  • You item stucture is now:

    ApplicationWindow {
        Item{
            GridLayout {}
            Button{} }}
    

    not

    ApplicationWindow {
        Item{
            GridLayout {
                Button{} }}}
    

    That causes number 2. The button isn't inside the layout.


  • Qt Champions 2017

    in addition to previous post, if you want to achieve the way your code exist, here is the sample.

    MyGrid.qml
    import QtQuick 2.7
    import QtQuick.Layouts 1.3

    Item {
    property alias lyout: lay
    GridLayout {
    id: lay
    anchors.fill: parent
    }
    function addObject(val){
    val.parent = lay
    }
    }

    =========
    MyApp.qml

    import QtQuick 2.5
    import QtQuick.Controls 2.0
    import QtQuick.Layouts 1.3

    ApplicationWindow {
    id: root
    visible: true
    height: 1024
    width: 768

    MyGrid {
        anchors.fill: parent
        id : myGrid
        Button {
            id : b1
            text : "Ali"
            Layout.alignment: Qt.AlignBottom | Qt.AlignRight
            parent : myGrid.lyout
        }
        Button {
            id : b2
            text : "Pradeep"
            Layout.alignment: Qt.AlignBottom | Qt.AlignRight
            parent : myGrid.lyout
        }
        Component.onCompleted:{
    

    // myGrid.addObject(b1)
    // myGrid.addObject(b2)
    // b1.parent = myGrid.lyout
    // b2.parent = myGrid.lyout
    }
    }
    }



  • @Eeli-K Are you sure about that item structure? When aliasing the default property is should be added with the aliased origin as its parent no?



  • @Phataas I see. Well, I don't know about that, but you can see the actual structure if you debug the qml code with QtCreator.


Log in to reply