Jak mít sticky záhlaví pro GridLayout v Flickable

0

Otázka

Mám GridLayout obydlený Opakovač (TableView nesedí mé potřeby), uvnitř Flickable, takže můžu posouvat obsah.

Chci, aby moje GridLayout mít záhlaví, které mohu snadno přidáním Texts před mým Opakovač, jako 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)
                    }
                }
            }
        }
    }
}

Nicméně rád bych, aby moje hlavička bude "sticky" / "zmrazené", tj. zůstávají viditelné při posouvání Flickable. Bych mohl vytvořit svou hlavičku mimo Flickable, nicméně GridLayout nedává mi poslední řadě velikostí, takže nemohu pozici záhlaví moje texty.

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

Nejlepší odpověď

0

Je trochu zašlé, ale našel jsem řešení.

Nejprve vytvořte "fiktivní" záhlaví, které jsou Items. Můžete si nastavit jejich Layout.minimalWidth být šířka v záhlaví textu, pokud potřebujete.

Pak, v Položce před Flickable, vytvořit záhlaví, ujistěte se, že je vodorovně zarovnán s mřížkou, a pozice prvků pomocí x hodnoty záhlaví.

Konečně, nastavte zápornou marží na Flickable odstranit nadbytečné řadě přidány dummy záhlaví.

Také jsem se pokusil pomocí fillWidth: true na nechápavé a pak nastavení width každé záhlaví položky, ale nevýhodou je, že se mění šířka sloupců tabulky.

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 jiných jazycích

Tato stránka je v jiných jazycích

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