Cum de a avea un antet lipicios pentru un GridLayout într-un Flickable

0

Problema

Am un GridLayout populat de un Repetor (un TableView nu se potrivește cu nevoile mele), în interiorul unui Flickable deci pot derula conținutul.

Vreau să-mi GridLayout să aibă un antet, care pot cu ușurință prin adăugarea de Texts înainte de Repetor de genul asta:

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

Cu toate acestea, aș dori să-mi antet să fie "lipicios" / "înghețate", adică să rămână vizibile atunci când defilare Flickable. Am putea crea antet în afara Flickable, cu toate acestea GridLayout nu-mi dă ultimul rând dimensiuni, astfel încât nu pot poziția mea antet texte.

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

Cel mai bun răspuns

0

E un pic mai ieftin, dar am găsit o soluție.

În primul rând, a crea "dummy" antete care sunt Items. Puteți face un set lor Layout.minimalWidth pentru a fi lățimea de text antet, dacă aveți nevoie să.

Apoi, într-un Articol înainte de Flickable, de a crea antet, asigurați-vă că acesta este orizontal aliniat cu grila, și poziția elementelor folosind x valorile de antet.

În cele din urmă, a stabilit o marjă negativă de pe Flickable pentru a elimina străine rând adăugată de manechin cap.

De asemenea, am încercat, folosind fillWidth: true pe manechine și apoi setarea width de fiecare antet elemente, dar dezavantajul este că se modifică tabelul lățimea coloanei.

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

În alte limbi

Această pagină este în alte limbi

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