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. Table header ResizeToContents and Stretch ?
Forum Updated to NodeBB v4.3 + New Features

Table header ResizeToContents and Stretch ?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.4k 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.
  • cpperC Offline
    cpperC Offline
    cpper
    wrote on last edited by
    #1

    Hi, is it possible to achieve something like a mix of the two ResizeModes of QHeaderView ? Firstly, I want the columns to be resized in such manner, that all content is visible. This is achieved using the ResizeToContents flag. Secondly, I want to stretch all columns(by the same factor) until the table fits the entire width of the widget.
    Is this doable ?

    1 Reply Last reply
    0
    • gde23G Offline
      gde23G Offline
      gde23
      wrote on last edited by
      #2

      I dont't think there is a build in function for that, but you can do the following:

      • get total view width
      • calculate the size of each table entry (using QFontMetrics for text and size() for icons and so on
      • calculate the remaining width after each column has the width it needs for the content and distribute it equally to all the widths
      • setting the size by calling setColumnWidth() for each column
      1 Reply Last reply
      1
      • cpperC Offline
        cpperC Offline
        cpper
        wrote on last edited by
        #3

        Thanks @gde23. Using your ideas, I think I found a better(easier) solution:

        1. Don't set the section resize mode to QHeaderView::ResizeToContents
        2. After modifying data in the table do:
        beerTable.resizeColumnsToContents();
        
        //followed by
        
        int widgetWidth = beerTable.viewport()->size().width();
        int tableWidth = 0;
        
        for(int i = 0; i<beerTable.columnCount(); ++i)
            tableWidth += beerTable.horizontalHeader()->sectionSize(i); //sections already resized to fit all data
        
        double scale = (double)widgetWidth/tableWidth;
        for(int i = 0; i<beerTable.columnCount(); ++i)
            beerTable.setColumnWidth(i, beerTable.horizontalHeader()->sectionSize(i) * scale);
        
        1 Reply Last reply
        1

        • Login

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