Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QT QPushButton with an icon and overlaid centered text

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

like image 587
Miky Mike Avatar asked Apr 20 '11 19:04

Miky Mike


1 Answers

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.

like image 55
Dave Mateer Avatar answered Oct 16 '22 21:10

Dave Mateer