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 and HorizontalHeaderView column widths do not match, even with syncView property set
Qt 6.11 is out! See what's new in the release blog

TableView and HorizontalHeaderView column widths do not match, even with syncView property set

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 876 Views 1 Watching
  • 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.
  • Aleksey_KA Offline
    Aleksey_KA Offline
    Aleksey_K
    wrote on last edited by Aleksey_K
    #1

    ❓ Problem: TableView and HorizontalHeaderView column widths do not match, even with syncView

    I'm using Qt Quick (Qt 6.8) with a TableView and a HorizontalHeaderView. I’ve set up syncView so that the header and table should stay in sync:

    HorizontalHeaderView {
        id: header
        Layout.fillWidth: true
        syncView: tableView
    }
    

    The TableView defines a columnWidthProvider to calculate dynamic widths, including a "stretch" column (e.g. column 2):

    TableView {
        id: tableView
        Layout.fillWidth: true
    
        columnWidthProvider: function (column) {
            let min = minimalColumnWidth(column)
            if (column === 2) {
                return stretchColumnWidth()
            }
            return min
        }
    }
    

    However, some columns (especially columns 2 and 3) show mismatched widths between the table and header. This happens even though syncView is set.

    1 Reply Last reply
    0
    • Aleksey_KA Offline
      Aleksey_KA Offline
      Aleksey_K
      wrote on last edited by
      #2

      ✅ Solution: Force layout recalculation after width changes

      To ensure layout syncs correctly, you need to explicitly trigger layout updates when the TableView or HorizontalHeaderView resizes.

      Fix: Add onWidthChanged: tableView.forceLayout() in both the TableView and the HorizontalHeaderView.

      🔧 Updated code:

      TableView {
          id: tableView
      
          columnWidthProvider: function (column) {
              // Your width logic
          }
      
          onWidthChanged: tableView.forceLayout()
      }
      
      HorizontalHeaderView {
          id: header
          syncView: tableView
      
          onWidthChanged: tableView.forceLayout()
      }
      

      ✅ Even in HorizontalHeaderView, you call **tableView.forceLayout()**, not header.forceLayout().


      🧠 Why this works

      • columnWidthProvider often runs before tableView.width is fully initialized or updated.
      • forceLayout() triggers Qt to re-run layout and width calculations with correct sizing.
      • syncView can now properly sync header and table widths based on the final layout pass.

      ✅ Result

      Now the header and table stay in perfect sync, even when:

      • Resizing the window
      • Stretching a specific column to take remaining space
      • Changing content dynamically
      1 Reply Last reply
      0
      • Aleksey_KA Aleksey_K has marked this topic as solved on

      • Login

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