0
votes

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? - RobbieE
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

1 Answers

0
votes

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