PDA

View Full Version : Autoscaling widget font on resize



SeanM
10th March 2014, 17:27
I'm trying to develop a container widget that will automatically recalculate font sizes of child widgets when the container & child widgets get resized. The purpose of this widget is that my application is being used in a laboratory environment where the users are often working on a remote device that communicates to my application in real-time. The remote device is often far enough from the host computer that they'd like to be able to increase the font size so that they can read the live data from where they're standing. I know I could do this with some sort of font dialog/spin box setup, but I thought it would be easier if the font size would automatically increase/decrease to fit the widget size(s) based on string length.

The widget should basically function like an QHBoxLayout, keeping the child widgets in a horizontal row. The child widgets are a mix of QLabel, QRadioButton, QSpinBox, QCheckBox, QPushButton, QLineEdit. So I've reimplemented resizeEvent() for my container widget and am using QFontMetrics to determine what the largest font size is that will work based on the sizes of all the child widgets.

I've got a functioning demo which I've attached, but it's got a couple of issues:

Fonts/widgets gets bigger, but not smaller. When the application launches everything starts out 8 point (at least on my machine). When I drag the resize handles to make the main window larger the font size increases (yay!). But once the window has increased in size, there doesn't seem to be a way to drag the window smaller again; my container widget never gets any resize events when I'm trying to shrink the window. I'm assuming this is a QSizePolicy issue, but I think I've tried all of them and haven't got it to work the way I want. Ideally the container widget is willing to be as big or small as necessary, but always tries to make the font as large as possible, once all the child widget sizes have been calculated.
For widgets that have non-text decorations (like the buttons in a QCheckBox or QRadioButton, etc.) there doesn't seem to be a way to query for the width (in pixels) of the text-only portion of that widget. QRadioButton::size() returns the pixel size of the entire widget (button + text label), which then screws up the calculations of how big the font can be based on string length since it thinks the available space for text is wider than it actually is.