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

How align Listview's delegate text to customized title text?



  • import QtQuick 2.0
    
    Item
    {
        id: root
        width: 1921
        height: 1081
    
        Column
        {
            x: 328
            y: 184
            Item
            {
                id: tableTitle
                width: 1593
                height: 91
                Text {
                    id: ttWellId
                    x: 90
                    y: 31
                    text: "Well ID"
                }
                Text {
                    id: ttSample
                    x: 276
                    y: 31
                    text: "Sample"
                }
            }
    
            ListView
            {
                width: 1593
                height: 785
                clip: true
                model: ListModel {
                    ListElement{
                        name: "A1";
                        sample: "sample1";
                    }
                    ListElement{
                        name: "A2";
                        sample: "sample2";
                    }
                }
                delegate: resultDelegate
    
                Component
                {
                    id: resultDelegate
                    Item
                    {
                        id: wellInfoItem
                        height: 60
                        width: 1592
                        Text
                        {
                            text: name
                            anchors.verticalCenter: parent.verticalCenter
                            anchors.horizontalCenter: ttWellId.horizontalCenter
                        }
                        Text {
                            text: sample
                            anchors.verticalCenter: parent.verticalCenter
                            anchors.horizontalCenter: ttWellId.horizontalCenter
                        }
                    }
                }
            }
        }
    }
    

    I need resultDelegate's text align to text in the tableTitle. But anchors.horizontalCenter: ttWellId.horizontalCenter's effect as following:
    4daa9cc6-ced7-499f-9a66-a1b0d32a3e16-image.png
    The effect I want is as follows :
    de39afaf-448d-4646-8a14-c211a655ae23-image.png
    please ignore the multiple row and focus on contents are aligned horizontally to the header. And I got a error hint: QML Text: Cannot anchor to an item that isn't a parent or sibling.



  • I have a solution, aligning field to header title and setting the same width, then we could set same alignment for Text item.
    But my .qml file from Qt Designer Stutido, They had been given in the form of fixed coordinates at most of the time, adjusting for my solution is too terrible.


Log in to reply