how to calculate a fixed widget size?
-
wrote on 1 Aug 2023, 04:02 last edited by
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);
-
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);
wrote on 1 Aug 2023, 07:07 last edited by ChrisW67 8 Jan 2023, 07:12@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_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.
wrote on 1 Aug 2023, 09:05 last edited by@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/3