Ako majú lepkavé hlavičky pre GridLayout v Flickable

0

Otázka

Mám GridLayout obývaný Repeater (a TableView nesedí moje potreby), vo vnútri Flickable tak som sa môžete posúvať obsah.

Chcem, aby moje GridLayout mať hlavičku, ktorú môžem jednoducho pridaním Texts kým môj Repeater, ako je tento:

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)
                    }
                }
            }
        }
    }
}

Avšak rád by som v mojej hlavičky, že "lepkavé" / "zmrazené", t. j. zostávajú viditeľné pri rolovaní sa Flickable. Mohol by som vytvoriť moja hlavička mimo Flickable, avšak GridLayout nedáva mi záverečnej riadok veľkosti, takže nemôžem pozície mojej hlavičky texty.

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

Najlepšiu odpoveď

0

Je to trochu hacky, ale našiel som riešenie.

Najprv, vytvorte "fiktívne" hlavičky, ktoré sú Items. Môžete nastaviť, aby ich Layout.minimalWidth ak chcete byť šírka hlavičky text, ak potrebujete.

Potom, v Položke pred Flickable, vytvoriť hlavičku, uistite sa, že je horizontálne zarovnaný s mriežkou, a polohy prvkov pomocou x hodnoty hlavičky.

Nakoniec, zadajte zápornú marže na Flickable na odstránenie nadbytočných riadok pridaný figuríny hlavičky.

Tiež som sa snažil pomocou fillWidth: true na nechápavo a potom nastavenie width z každej hlavičke položky, ale nevýhodou je to, že upravuje tabuľka šírku stĺpca.

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

V iných jazykoch

Táto stránka je v iných jazykoch

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