Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. TableView with horizontal header - Column Width
Forum Updated to NodeBB v4.3 + New Features

TableView with horizontal header - Column Width

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
1 Posts 1 Posters 422 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    davidesalvetti
    wrote on last edited by
    #1

    Hi all,

    I'm developing a crossplatform application. Rigth now, I'm working on Window 10, Qt 5.15.2 MinGw 32bit. In my app I have a TableView on the qml side and a model derived from QSqlQueryModel on c++ side. I'm using QtQuick 2.15.

    The TableView contains the data from a database but I needed an Header to display the column name, so I added a Row component in the TableView. The problem is with the function columnWidthProvider. I know from the doc that if I do not specify nothing, then the column's width is adjusted to make the items of a column fit inside. But I need to know that value so that I can give it to the Items inside the Row Header component, so that the columns are alligned to their corrispective "name".

    This is my following code:

    TableView {
                id: tableViewid
    
                anchors {
                    top: parent.top
                    left: parent.left
                    right: parent.right
                    bottom: goToLapsButtonId.top
                    topMargin: 10
                    leftMargin: 10
                    rightMargin: 10
                    bottomMargin: 10
                }
    
                columnWidthProvider: function (column) {
                    if (column === 0)
                        return 0;
                }
                rowHeightProvider: function (column) {}
                topMargin: columnsHeader.implicitHeight
    
                model: masterController.ui_databaseController.ui_tableModel
                clip: true
    
                property int selectedRow : -1
                delegate: Rectangle {
                    id: modelrect
                    implicitWidth: textId.implicitWidth + 50
                    implicitHeight: textId.implicitHeight + 10
                    Text {
                        id: textId
                        text: display
                        anchors.fill: parent
                        color: row == tableViewid.selectedRow ? 'white' : 'black'
                        font.bold: row == tableViewid.selectedRow
                        font.pixelSize: 14
                        verticalAlignment: Text.AlignVCenter
                        horizontalAlignment: Text.AlignHCenter
                    }
    
                    color: row == tableViewid.selectedRow ? "#AA0000ff" : "#AAffffff"
    
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            if (tableViewid.selectedRow == row)
                                tableViewid.selectedRow = -1
                            else
                                tableViewid.selectedRow = row
                        }
                    }
                }
    
                Row {
                    id: columnsHeader
                    y: tableViewid.contentY
                    z: 2
                    Repeater {
                        id: headerItems
                        model: tableViewid.columns > 0 ? tableViewid.columns : 1
                        Label {
                            width: tableViewid.columnWidthProvider(modelData)
                            height: 20
                            text: masterController.ui_databaseController.ui_tableModel.headerData(modelData, Qt.Horizontal)
                            color: '#ffffff'
                            font.pixelSize: 15
                            padding: 10
                            verticalAlignment: Text.AlignVCenter
                            horizontalAlignment: Text.AlignHCenter
    
                            background: Rectangle {
                                color: "#000022"
                            }
                        }
                    }
                }
    
                onModelChanged: forceLayout()
            }
    

    Any suggestion on how should I procede? I managed to do the opposite, using this function for columnWidthProvider:

    columnWidthProvider: function (column) {
                    if (column === 0)
                        return 0;
    
                    return headerItems.itemAt(column).implicitWidth + 10
                }
    

    In this case the columns name are alligned to the columns value, but if an item inside a column is larger that the column name then is cutted out.

    Thanks for the help.

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved