Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Setting size of QTableView columns to fit the contents programatically
Qt 6.11 is out! See what's new in the release blog

Setting size of QTableView columns to fit the contents programatically

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 626 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.
  • P Offline
    P Offline
    Pavel Bazika
    wrote on last edited by
    #1

    Hi,

    I'm trying to set columns width of QTableView to fit displayed contents.

    Using QHeaderView::ResizeToContents is not an option, because the model displayed by QTableView represent quite big data on remote server. QHeaderView::ResizeToContents causes all data is read first to get the biggest width, which is something to be avoided (otherwise I could use QTableWidget).

    Instead I'm using a proxy where I monitor data passing thru and resize the column when neccesary. The problem is, that I don't know how to get the proper width. I tried

    QFontMetrics{ tableWidget->font() }.horizontalAdvance(str)
    

    and

    QFontMetrics{ tableWidget->font() }.boundingRect(str).width()
    

    and then I call

    tableWidget->horizontalHeader()->resizeSection()
    

    but the value from font metrics is not big enough and the string still doesn't fit into the cell.

    Thanks for any help

    Pavel

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You have to add also some margins left and right of the text.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      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