Como ter um pegajoso cabeçalho para um GridLayout em um Flickable

0

Pergunta

Eu tenho um GridLayout preenchido por um Repetidor (um TableView não atender minhas necessidades), dentro de uma Flickable para que eu possa percorrer o conteúdo.

Eu quero o meu GridLayout para ter um cabeçalho, que eu posso facilmente adicionando Texts antes de minha Repetidor como este:

import QtQuick 2.0
import QtQuick.Layouts 1.12

ColumnLayout {
    width: 200
    height: 200

    Flickable {
        Layout.fillWidth: true
        Layout.preferredHeight: 200
        contentWidth: width
        contentHeight: grid.height
        clip: true

        GridLayout {
            id: grid
            columns: 3
            columnSpacing: 10

            function reparentChildren(source, target) {
                while (source.children.length) {
                    source.children[0].parent = target;
                }
            }

            // Header
            Text {
                text: "Header 0"
            }
            Text {
                text: "Header 1"
            }
            Text {
                text: "Header 2"
            }

            // Data
            Repeater {
                model: 50

                Item {
                    Component.onCompleted: grid.reparentChildren(this, grid)
                    Text {
                        text: "Column 0, %1".arg(modelData)
                    }
                    Text {
                        text: "Column 1, %1".arg(modelData)
                    }
                    Text {
                        text: "Column 2, %1".arg(modelData)
                    }
                }
            }
        }
    }
}

No entanto, eu gostaria que o meu cabeçalho para ser "pegajoso" / "congelados", isto é, permanecem visíveis durante a rolagem da Flickable. Eu posso criar meu cabeçalho fora do Flickable, no entanto, o GridLayout não me dá a linha final tamanhos, por isso eu não posso posição do meu cabeçalho textos.

qml qt
2021-11-23 10:31:17
1

Melhor resposta

0

É um pouco hacky, mas eu achei uma solução.

Primeiro, criar o "fictício" cabeçalhos que são Items. Você pode fazer o ajuste de suas Layout.minimalWidth para ser a largura do texto do cabeçalho, se você precisa.

Em seguida, em um Item antes de o Flickable, criar seu cabeçalho, verifique se ele está alinhado horizontalmente com a grade, e a posição os elementos usando o x valores do cabeçalho.

Finalmente, defina uma margem negativa Flickable para remover o estranho linha adicionada pelo fictício cabeçalhos.

Eu também tentei usar fillWidth: true em manequins e, em seguida, definindo o width de cada cabeçalho de itens, mas a desvantagem é que ele modifica a tabela de largura de coluna.

import QtQuick 2.0
import QtQuick.Layouts 1.12

ColumnLayout {
    width: 200
    height: 200

    // Header
    Item {
        Layout.minimumHeight: childrenRect.height
        Layout.fillWidth: true
        Text {
            id: header0
            x: headerDummy0.x
            anchors.top: parent.top
            text: "Header 0"
        }
        Text {
            id: header1
            x: headerDummy1.x
            anchors.top: parent.top
            text: "Header 1"
        }
        Text {
            id: header2
            x: headerDummy2.x
            anchors.top: parent.top
            text: "Header 2"
        }
    }

    Flickable {
        Layout.fillWidth: true
        Layout.preferredHeight: 200
        contentWidth: width
        contentHeight: grid.height
        clip: true
        // Eliminate the first row, which are the dummies
        topMargin: - grid.rowSpacing

        GridLayout {
            id: grid
            columns: 3
            rowSpacing: 50
            columnSpacing: 10

            function reparentChildren(source, target) {
                while (source.children.length) {
                    source.children[0].parent = target;
                }
            }

            // Header dummies
            Item {
                id: headerDummy0
                Layout.minimumWidth: header0.implicitWidth
            }
            Item {
                id: headerDummy1
                Layout.minimumWidth: header1.implicitWidth
            }
            Item {
                id: headerDummy2
                Layout.minimumWidth: header2.implicitWidth
            }

            // Data
            Repeater {
                model: 50

                Item {
                    Component.onCompleted: grid.reparentChildren(this, grid)
                    Text {
                        text: "Column 0, %1".arg(modelData)
                    }
                    Text {
                        text: "Column 1, %1".arg(modelData)
                    }
                    Text {
                        text: "Column 2, %1".arg(modelData)
                    }
                }
            }
        }
    }
}
2021-11-23 10:31:17

Em outros idiomas

Esta página está em outros idiomas

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................