0

I attempted to display a table in the window. My table has a total of 4 columns. I hope the column width of the table can be more adaptive to the width of my window (the window width will change), that is, the left border of the table coincides with the left border of the window, and the right border of the table coincides with the right border of the window. The column widths in the table can be modified by dragging with the mouse. I tried by anchoring, but it didn't work at all. In non-QML (i.e., c), this functionality can be achieved through the QTableView->horizontalHeader()->setSectionResizeMode() function

Question: How can such an effect be achieved in QML?

The version of QT I'm using is 6.8.3

The following is my QML code

import QtQuick
import Qt.labs.qmlmodels
import QtQuick.Controls

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("table modle view")


    TableModel {
        id: contactsModel

        TableModelColumn { display: "firstName" }
        TableModelColumn { display: "lastName" }
        TableModelColumn { display: "age" }
        TableModelColumn { display: "phoneNumber" }

        rows: [
            {
                firstName: "John",
                lastName: "Doe",
                age: "49",
                phoneNumber: "111-111-1111"
            },
            {
                firstName: "Jane",
                lastName: "Doe",
                age: "48",
                phoneNumber: "222-222-2222"
            },
            {
                firstName: "George",
                lastName: "Doe",
                age: "12",
                phoneNumber: "333-333-3333"
            },
        ]
    }


    Component {
        id: contactsDelegate

        Rectangle {
            id: delegateRect

            required property bool current

            implicitWidth: 100
            implicitHeight: 50
            color: current ? "pink" : "lightgray"
            border.width: current ? 2 : 0
            TableView.editDelegate: contactsEditDelegate

            Text {
                text: model.display
                padding: 12
                anchors.fill: parent
            }
        }
    }

    ScrollView {
        id: scrollView

        anchors.top: contactsHHeader.bottom
        anchors.bottom: parent.bottom
        anchors.left: contactsVHeader.right
        anchors.right: parent.right



        TableView {
            id: contactsView

            anchors.fill: parent


            columnSpacing: 1
            rowSpacing: 1
            resizableColumns: true
            resizableRows: true

            model: contactsModel
            delegate: contactsDelegate

            selectionModel: ItemSelectionModel {
                model: contactsView.model
            }


        }
    }

    HorizontalHeaderView {
        id: contactsHHeader

        anchors.left: parent.left
        anchors.right: parent.right
        anchors.leftMargin: contactsVHeader.width
        anchors.top: parent.top

        implicitHeight: 50
        syncView: contactsView
        textRole: "display"


        model: ListModel {
            ListElement{display: "First Name"}
            ListElement{display: "Last Name"}
            ListElement{display: "Age"}
            ListElement{display: "Phone Number"}
        }

        delegate: Rectangle
        {

            color: "#168bcd"
            Text {
                anchors.centerIn: parent
                text: model.display
                padding: 12

            }
        }

    }

    VerticalHeaderView{
        id: contactsVHeader

        anchors.left: parent.left
        anchors.top: parent.top
        anchors.topMargin: contactsHHeader.height

        syncView: contactsView
    }

    Component {
        id: contactsEditDelegate

        TextField {
            anchors.fill: parent
            text: model.display

            horizontalAlignment: TextInput.AlignHCenter
            verticalAlignment: TextInput.AlignVCenter

            TableView.onCommit: {
                let index = TableView.view.index(model.row, model.column)
                model.display = text
            }
        }
    }

}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.