Results 1 to 3 of 3

Thread: Max height of a font and QHeaderView::resizeMode

  1. #1
    Join Date
    Jul 2012
    Posts
    224
    Thanks
    18
    Thanked 14 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Max height of a font and QHeaderView::resizeMode

    Hey,

    I have a QTableWidget, whose row-height I prefer to be as compact as possible without compromising the readability of the text-cell-content.
    To achieve this I call

    Qt Code:
    1. table->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    To copy to clipboard, switch view to plain text mode 

    Works perfect, but turns out to be very slow with larger models, to the point where I am looking at a blacked out window for seconds.
    That makes sense, bc it has to look at fonts and styles etc and determine the height of many rows.


    I have looked through the code and found the "setResizeContentsPrecision" method, however, even with very low values it isnt much of an improvement.

    I *could* just do setSectionResizeMode(QHeaderView::Fixed) and then hardcode my preferred height.
    But that would probably destroy UX across platforms, because this is really style and font dependent.



    So the quest is to find a good, compact height. If I do setSectionResizeMode(QHeaderView::Fixed) and specify a height manually:


    1. leaving the default the row-height is absurdly large, leaving tons of empty space.
    2. if I use QFontMetric(font).lineheight() or QFontMetric(font).lineSpacing() then the height is too compact - it cuts off the lower pixels of a "p" or "q" or "g", for example. That makes the contents hard to read.
    3. setDefaultSectionSize(QFontMetrics(table->font()).boundingRect("WIqgppÄ").height()); has the same problem - see this image for how it cuts off the last pixel(s) from "g":

    https://i.imgur.com/KMpLZWV.png

    i am on Qt 5.12 and Ubuntu18.04, but it's a cross platform app.

    Sorry if this question isnt specific enough...

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,011
    Thanks
    233
    Thanked 636 Times in 626 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Max height of a font and QHeaderView::resizeMode

    Works perfect, but turns out to be very slow with larger models, to the point where I am looking at a blacked out window for seconds.
    That makes sense, bc it has to look at fonts and styles etc and determine the height of many rows.
    I thought that QTableView only did calculations for what was actually displayed on screen, not the entire model. I could be wrong about this, or maybe QTableWidget behaves differently since it wraps its own model.

    It may not be feasible for you, but maybe you could try using your own model and QTableView instead. For the model, you would need to implement Qt::SizeHintRole in the QAbstractItemModel::data() method to return the appropriate QSize for the row. You would also have the advantage that you could precompute the size and store the values in your model, and only update them when font or style changes.
    Last edited by d_stranz; 28th February 2019 at 00:24.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. The following user says thank you to d_stranz for this useful post:

    tuli (28th February 2019)

  4. #3
    Join Date
    Jul 2012
    Posts
    224
    Thanks
    18
    Thanked 14 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Max height of a font and QHeaderView::resizeMode

    As per the documentation I should be able to force it to only use the displayed rows by calling setResizeContentsPrecision(0), but that doesnt take the pressure off things... I will look into how much it actually accesses.

    And I was sloppy in my previous post: I am not using a QTableWidget but indeed a QTableView with my own model.

    For the model, you would need to implement Qt::SizeHintRole
    I think it should be possible to find a constant one-fits-all value of line-height here. I dont mind wasting a few pixels here and there, but the default height is like 2x of a reasonable size.
    For example, in practice and on my system this does nicely:


    Qt Code:
    1. table->verticalHeader()->setSectionResizeMode(QHeaderView::Fixed);
    2. table->verticalHeader()->setDefaultSectionSize(QFontMetrics(table->font()).lineSpacing() + 4);
    To copy to clipboard, switch view to plain text mode 


    The +4 basically ensure there's enough space so the last pixels dont get cut off. But it bothers me a bit to hardcode such arbitrary values, when the final result depends on Fonts and Styles.

Similar Threads

  1. How to set font size to given height?
    By Szyk in forum Qt Quick
    Replies: 0
    Last Post: 16th February 2018, 12:46
  2. how to change the height of QHeaderView?
    By TorAn in forum Qt Programming
    Replies: 1
    Last Post: 13th March 2015, 08:43
  3. QHeaderView eliding versus font size
    By davethomaspilot in forum Newbie
    Replies: 0
    Last Post: 2nd February 2013, 01:40
  4. Font Height and width based on font size
    By Ghufran in forum Qt Programming
    Replies: 1
    Last Post: 31st July 2010, 08:02
  5. Replies: 2
    Last Post: 22nd January 2008, 16:10

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.