19

How to change the IconSize of QToolButton.

button1->setIcon(QIcon("download.jpg"));
button1->setFixedSize(100,100);

By using above code button size is getting change but icon inside the button is not changing.

Maxim Makhun
  • 2,197
  • 1
  • 22
  • 26
Suresh
  • 745
  • 4
  • 11
  • 25

2 Answers2

27

How about

button1->setFixedSize(100,100);
button1->setIconSize(QSize(100, 100));

If your button lays on the toolbar then use

toolBar->setIconSize(QSize(100, 100));

instead of button icon size changing. If you want to have different sizes on the toolbar then vary them with setFixedSize(). Of course the maximal of them should be QToolBar icon size.

dvvrd
  • 1,679
  • 11
  • 20
  • Yes it worked. But if i want to set 1st button icon size to (100,100) and other to (200,200) then what should i do – Suresh Jan 15 '14 at 10:15
  • The size in toolBar is the maximum size toolbar icons can have. Set it to some maximum value and use lower (different) values for icons. – wolf9000 Jan 15 '14 at 10:19
  • 1
    @Rupesh, edited the answer. In that case you should assign (200, 200) to toolbar and (100, 100) to button`s fixed size – dvvrd Jan 15 '14 at 10:23
3

From https://qt-project.org/doc/qt-5/qabstractbutton.html#iconSize-prop

You can try using

button1->setIconSize(QSize(100, 100));

Or you can give the button1 size as an argument,

button1->setIconSize(button1->size());

The only downside with this method is that the icons will not be scaled by more than 100% of their original size. If you want icons scaled up, you can try to reimplement the QToolButton::setIconSize method or, as a quick and dirty fix, resize the images using an image editor.

In case of using a QToolBar, use QToolBar::setIconSize method which sets the maximum size icons in the toolbar can have. The icons themselves can be of different size.

wolf9000
  • 141
  • 1
  • 9
  • Is your `button1` created using Qt Designer? Can you check if there are any properties you may have accidentally set (shown in bold) that may prevent the method from resizing the button? Is the button part of a layout or a grid? – wolf9000 Jan 15 '14 at 10:03
  • NO i have not created is using QTDesigner. QToolButton is a part of QToolBar and QtoolBar is a part of layout. Button size is enhancing but not icon size – Suresh Jan 15 '14 at 10:06
  • 1
    You should use QToolBar::setIconSize also. – wolf9000 Jan 15 '14 at 10:13