When I create a QPushButton with an Icon it by default displays the text to the right of the icon. Is there a way to have the text display underneath the icon?
+5
A:
If you're able to, the easiest thing to do is use a QToolButton instead:
QToolButton* button = new QToolButton(this);
button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
button->setIcon(myIcon);
button->setText("Sample text");
If that's not an option you could consider creating your own button widget, possibly derived from QPushButton or QAbstractButton. In this case you'll probably (I haven't tried it myself) want to focus most of your efforts on reimplementing paintEvent()
.
[Edit: read the comments for alternatives which are probably way simpler than this]
richardwb
2009-09-13 20:38:31
You can actually add a UI to a QPushButton if you want to. The automatic sizing can get tricky, but that way you can completely control where an icon and text label go.
Caleb Huitt - cjhuitt
2009-09-13 21:05:23
subclassing just for formatting is usually not worth the effort. A lot can be done just by applying styles. And while the documentation on the styles could be better, it works reasonably well and usually as expected.
Harald Scheirich
2009-09-13 21:11:27
This was exactly what I was trying to do. Thanks!
DHamrick
2009-09-13 22:20:48