PDA

View Full Version : Filling QToolButton with an image



i92guboj
22nd August 2013, 16:04
Hello.

Is there any way to fill the whole button with its assigned icon?

I am doing it this way, so that images should exactly match the size of the buttons, but there's always an empty margin around the images.



QSize size = ui->plano_1->size();
qDebug() << Q_FUNC_INFO << QString("icon size: %1,%2").arg(size.width()).arg(size.height());
ui->plano_1->setIcon(QIcon(myBudget->getPlano(1)));
ui->plano_1->setIconSize(size);
ui->plano_1->setContentsMargins(0, 0, 0, 0);


As you see, I also tried using setContentsMargins(0,0,0,0). But it doesn't seem to make any difference.

Thank you for any response :)

Santosh Reddy
23rd August 2013, 10:39
QSize size = ui->plano_1->size();
Size of the tool button is valid only after the tool button is visible (i.e show() is called). Size of any widget is not valid before show() is called on it or it's parent.

setContentsMargins() is only used by the layout manager and does not affect the widget content.

avner
1st June 2014, 08:32
Hi,
I didn't manage to strech the icon to more than it's original size, so if you icon size is 16x16, this will be the icon's bigget size.
What you can do, is to change the button's size, and make the margins smaller. In my main window, the button's default size is 32x32, and a lot of the button's area is empty.
Once I changed the button's maximum size, it looked better.

myButton->setMaximumSize(QSize(16, 16));

Now the icon is covering almost the whole button.

Good luck!