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
Forum Updated to NodeBB v4.3 + New Features

Setting size of QTableView columns to fit the contents programatically

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 450 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 9 Jan 2021, 19:05 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
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 9 Jan 2021, 19:07 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

      1/2

      9 Jan 2021, 19:05

      • Login

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