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. how to calculate a fixed widget size?
Forum Update on Monday, May 27th 2025

how to calculate a fixed widget size?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 224 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.
  • M Offline
    M Offline
    M_Qt
    wrote on 1 Aug 2023, 04:02 last edited by
    #1

    i have a widget named Tdw that extends QWidget, its just a container which holds a button and label in a vbox layout.

    the button and label are meant to hold a single character.
    to fit neatly in a grid every Tdw has to be the same fixed size.

    how to calculate the size of Tdw is the problem. i tried adding sizehints together and that almost works, but some fonts are making the size come out wrong and it's causing the Tdw to be way too tall.

    the vertical size hint is 119 for the button when i select the font "Cambria Math" in 16pt size. for fonts that behave correctly, the vertical size hint is 25 to 30.

    here is how i'm doing it now:

    QFontMetrics fm(tdw_font);
    int tdwWidth = fm.boundingRect("W").width() +2;
    int tdwHeight = tdw->label->sizeHint().height() + tdw->button->sizeHint().height();
    tdw->setFixedSize(tdwWidth, tdwHeight);
    
    C 1 Reply Last reply 1 Aug 2023, 07:07
    0
    • M M_Qt
      1 Aug 2023, 04:02

      i have a widget named Tdw that extends QWidget, its just a container which holds a button and label in a vbox layout.

      the button and label are meant to hold a single character.
      to fit neatly in a grid every Tdw has to be the same fixed size.

      how to calculate the size of Tdw is the problem. i tried adding sizehints together and that almost works, but some fonts are making the size come out wrong and it's causing the Tdw to be way too tall.

      the vertical size hint is 119 for the button when i select the font "Cambria Math" in 16pt size. for fonts that behave correctly, the vertical size hint is 25 to 30.

      here is how i'm doing it now:

      QFontMetrics fm(tdw_font);
      int tdwWidth = fm.boundingRect("W").width() +2;
      int tdwHeight = tdw->label->sizeHint().height() + tdw->button->sizeHint().height();
      tdw->setFixedSize(tdwWidth, tdwHeight);
      
      C Offline
      C Offline
      ChrisW67
      wrote on 1 Aug 2023, 07:07 last edited by ChrisW67 8 Jan 2023, 07:12
      #2

      @M_Qt What are you putting in the QLabel and the button?

      This is what two 16-point fonts from Windows 11 produce in QFontMetrics::boundingRect("W"):

      0 "Cambria Math" QRect(0,-17 20x22)
      1 "Calibri" QRect(0,-16 19x22)
      

      The font is not the direct source of the height.

      M 1 Reply Last reply 1 Aug 2023, 09:05
      0
      • C ChrisW67
        1 Aug 2023, 07:07

        @M_Qt What are you putting in the QLabel and the button?

        This is what two 16-point fonts from Windows 11 produce in QFontMetrics::boundingRect("W"):

        0 "Cambria Math" QRect(0,-17 20x22)
        1 "Calibri" QRect(0,-16 19x22)
        

        The font is not the direct source of the height.

        M Offline
        M Offline
        M_Qt
        wrote on 1 Aug 2023, 09:05 last edited by
        #3

        @ChrisW67
        i am only putting a single uppercase character into the button and label, because this is for a type of single character display where i put them into a row in a grid layout. also neither the button or label have a QSizePolicy i haven't specified a min/max size for them either. i am wondering if that has anything to do with it.

        1 Reply Last reply
        0

        1/3

        1 Aug 2023, 04:02

        • Login

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