QT QPushButton with an icon and overlaid centered text

buttoncenteringimageqt

I recently took to Qt Programming and want to port my app to Qt.

I'm fiddling with QPushButtons and I've managed to display an image into the button and to set some text to the button but, whatever I do, with or without the designer,
I have the same problem, the text is aligned right to the icon instead of being overlaid over the icon.

addButton = new QPushButton();
addButton->setIcon(QIcon(":/AddButton.png"));
addButton->setText(tr("+"));
addButton->setFlat(true);
addButton->setIconSize(QSize(100,90));

What am I missing ?

I know there is the toolButton but it doesn't seem to have the "flat" property.

Any idea please ?

Thanks a lot,

Mike

Best Answer

If you are trying to use your image as a background image, you can use a style sheet:

addButton->setStyleSheet("background-image: url(:/AddButton.png);"
                         "background-repeat: no-repeat;"
                         "background-position: center center");

You'll just have to make sure the size of your button is at least as big as the image.