0

I am creating a custom QPushButton. If the button has an icon I want the icon centered on the button. If the button has text I want the text centered on the button. I can handle both of those cases. If the button contains both an icon and text I want the icon centered and the text drawn below the button, outside the button rectangle. I am drawing the button image using border-image in a stylesheet.

Inside MyPushButton::paintEvent() I calculate a rectangle and draw the icon using:

    style()->drawItemPixmap(&p, iconRect, Qt::AlignLeft | Qt::AlignVCenter, 
        this->icon().pixmap(iconRect.size()));

I then calculate a text rectangle below the button and do this:

    style()->drawItemText(&p, textRect, Qt::AlignCenter, (this->palette()),
        true, this->text(), QPalette::ButtonText );

but the text is not visible.

I think this must have something to do with designated "drawable" area, but I can't figure out how to extend that area so that the button image doesn't to fill the area where I want to place the text.

I can do this with a QStyle::drawControl() overload but I'm not sure why. When I trace the code into the CE_PushButtonLabel case the myStyle->rect has already been resized to be large enough to fit the button image and the text.

  • 1
    Have you tried using a [QToolButton](http://qt-project.org/doc/qt-5.0/qtwidgets/qtoolbutton.html)? – RobbieE May 31 '13 at 19:17
  • I looked into QToolButton, but I don't want the text to just appear beneath the icon. I want it to appear beneath the "apparent" button image. Imagine placing a QPushButton in Designer. Adding an "icon" property via the Property list. Then placing a QLabel underneath the bounds of the button. But the contents of the QLabel would reflect the "text" property of the button. I could probably do it this way, but surely there is a way to draw the button background and then change the extent of the button so I can add the text at a specified location? – user2429682 Jun 13 '13 at 19:45

1 Answers1

0

Looks like a job for QToolButton along with setToolButtonStyle(Qt::ToolButtonTextUnderIcon).

Jan Kundrát
  • 3,700
  • 1
  • 18
  • 29
  • I want the text under the button. I want the icon to be displayed on the button and the string returned by this->text() to be drawn beneath, giving the impression that it is a QLabel. The customer wants a custom button available through QtDesigner that works this way. When they enter the "text" property that string appears beneath the button while the "icon" appears centered on the button. – user2429682 Jun 13 '13 at 19:51