How to set and change a QPushbutton's text size in proportion to the size of the QPushbutton
-
Can someone please teach me how to make the size of the text of a QPushbutton change in proportion to the size of the QPushbutton? I have a QPushbutton that grows and shrinks in proportion to its parent QWidget. Changing the text size is a puzzle.
Is it possible to do this with QtDesigner? I have a feeling that it's not.
-
You will have to reimplement "QWidget::resizeEvent() ":http://doc.qt.nokia.com/4.7/qwidget.html#resizeEvent
-
Interesting. I'm working on that. I subclassed QPushButton and implemented resizeEvent() in my new class. It changes the point size of the font with setStyleSheet(). There are side effects.
Since I use QtCreator's Designer to build my UI, I told it that the QPushButton is really my subclass, ResizableTextQPushButton.
Setting the font size using setStyleSheet() makes the QPushButton forget the color of its text on Maemo and Symbian^3. On Maemo, using setStyleSheet() to set the text and color makes the QPushButton forget its font size.
In my application, the button is initially "Don't Panic" in green. When the button is pressed, I change it to "Panic!" in red. That also initiates a series of tasks. When the tasks are completed, I change the text back to green "Don't Panic". I use setStyleSheet() to change both the color and the text of the button.
On my N900 w/Maemo, using my subclass of QPushButton, my application opens with white text of the correct size instead of green text. When I push the button, the text changes to red but it's 12-point instead of the larger, button-filling size computed by resizeEvent(). When the tasks are completed, the text color changes to green and it remains at 12-point until I trigger resizeEvent() by rotating the phone. Then it changes the text to the correct size but the color is now white.
On my N8 w/Symbian^3, again using my subclass of QPushButton, the application opens with white text of the correct size. When I push the button, the text remains the correct size and the color changes from white to red to green. If I do not rotate the phone, the text remains the right color and size. When I rotate the phone to trigger resizeEvent(), the size is correct but the color changes to white.