Nmaster88
Nmaster88

Reputation: 1605

How to have columns of TableView ocupy all of the parent

How can we make the columns ocuppy all of the size of the parent? Why am i asking this? Because as we can see on a simple example the columns dont fill all of the size of table and this is ugly.

The only thing i dont want to happen to a column is to have it with less width than the contents of its column.

I want the solution to work for many columns.

How can we succesfully do it?

import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Controls 1.4
    import QtQuick.Layouts 1.3
import QtQuick.Controls.Styles 1.1

ApplicationWindow {
    id: window
    title: "Stack"
    visible: true
    width: 1400
    ListModel {
        id: libraryModel
        ListElement {
            title: "A Masterpiece"
            author: "Gabriel"
        }
        ListElement {
            title: "Brilliance"
            author: "Jens"
        }
        ListElement {
            title: "Outstanding"
            author: "Frederik"
        }
    }

    Page {
        id: page
        anchors.fill: parent
        TableView{
            style: TableViewStyle{
                handle: Rectangle {
                    implicitWidth: 15
                    implicitHeight: 15
                    color:  "#000000"
                }
                minimumHandleLength: 30
            }
            anchors.fill:parent
            TableViewColumn {
                role: "title"
                title: "Title"
                width: 100
            }
            TableViewColumn {
                role: "author"
                title: "Author"
                width: 200
            }
            model: libraryModel
            itemDelegate: Text
            {
              text: styleData.value
              elide: Text.ElideRight
            }
        }
    }
}

Has pointed on an answer something like

width: (table.width / table.columnCount) - 1

can be used but i dont want to happen in small widths for items/text to be cut i want the scroll bar to appear:

enter image description here

Upvotes: 0

Views: 64

Answers (1)

Massimo Callegari
Massimo Callegari

Reputation: 2107

Like this ? -1 is just to avoid seeing an annoying scrollbar (probably caused by margins/borders)

TableView{
    id: table
    anchors.fill:parent

    style: TableViewStyle{
        handle: Rectangle {
            implicitWidth: 15
            implicitHeight: 15
            color:  "#000000"
        }
        minimumHandleLength: 30
    }

    TableViewColumn {
        role: "title"
        title: "Title"
        width: (table.width / table.columnCount) - 1
    }
    TableViewColumn {
        role: "author"
        title: "Author"
        width: (table.width / table.columnCount) - 1
    }
    model: libraryModel
    itemDelegate: Text
    {
      text: styleData.value
      elide: Text.ElideRight
    }
}

Upvotes: 1

Related Questions