Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QML dynamic columns width

Tags:

qt

qml

I have TableView with 4 columns. When I enlarge width of main window, I needed enlarge first column width too.

Older way (non QML) was:

QTableView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch );

Now I do something like example bellow, but this is not exactly perfect.

  1. When I resize some column, this construction stop working. All columns stay in actual state and stop resizing when main window change his size.
  2. When I add new columns in future (forgot rewrite this codeine) or add some columns dynamically, this stop working too.

QML file: :

TableView {  

     id: ccPanel  
     model: ccModel
    TableViewColumn {
        id: ccName
        role: "name"
        title: "Name"
        width: ccPanel.width - ccSize.width - ccDate.width
    }
    TableViewColumn {
        id: ccSize
        role: "size"
        title: "Size"
        width: 60
    }
    TableViewColumn {
        id: ccDate
        role: "Date"
        title: "Datum"
        width: 85
    }
}

Do you know how to help me?

like image 791
exo Avatar asked Oct 23 '25 18:10

exo


1 Answers

To eliminate strange resizing, make the last column non-resizable manually and use the widthChanged signals of the columns as well as the signal from the TabView. The following code produces a behavior very close to the Qt TableView header.

TableView
{
    id: ccPanel
    model: ccModel
    anchors.fill: parent
    onWidthChanged:ccName.width = Math.max(100, ccPanel.width - ccSize.width - ccDate.width)

    TableViewColumn
    {
        id: ccName
        role: "name"
        title: "Name"
        onWidthChanged: ccDate.width = Math.max(60, ccPanel.width - ccSize.width - ccName.width)
    }
    TableViewColumn
    {
        id: ccSize
        role: "size"
        title: "Size"
        width: 60
        onWidthChanged: ccDate.width = Math.max(85, ccPanel.width - ccSize.width - ccName.width)
    }
    TableViewColumn
    {
        resizable: false
        id: ccDate
        role: "Date"
        title: "Datum"
        width: 85
    }
}
like image 68
Yoann Quenach de Quivillic Avatar answered Oct 27 '25 01:10

Yoann Quenach de Quivillic