PDA

View Full Version : Designing the display content of a push button



cookie1909
14th April 2009, 16:44
I'm using Qt 4.5, the description of QPushButton says: "Push buttons display a textual label, and optionally a small icon."

However I need more than just a label and an icon. I'd like to have a small number on the top left corner of the button, then in the middle I want to have an icon, then a label, then another icon. I know I can do this with Gtk by setting the button to be a container type and I can pretty much put anything in the button I want. Is there a way for me to do this in Qt? I can't seem to find it :(

Any pointer would be greatly appreciated!

wysota
14th April 2009, 23:53
You can place any widget inside any other widget in Qt (so to say each widget in Qt is implicitly a container) but it's better if you subclass the button and reimplement its paintEvent() to have a specialized class doing exactly what you want to achieve. Especially that only adding a number in the corner will be faster through subclassing that through composition. The class will have a total of about 10 lines of code and you can reuse it many times.

cookie1909
15th April 2009, 17:22
Thanks a lot for your reply. I will try it out. Just wondering, can I add more widgets to the button using Qt Designer?

wysota
16th April 2009, 00:01
No, Designer needs widgets to be explicitly marked as containers to allow such things. The regular usecase is that you don't want primitive widgets to be containters so it would only make it harder to use Designer if your widgets would pop up with wrong parents.

cookie1909
16th April 2009, 22:24
Great, thank you! I got it to work with some coding, like you said, 10 lines of code :)