Veera
Veera

Reputation: 341

How to Change the IconSize for Actions in QMenu?

I am trying to resize the Icons of QActions in the QMenu with the following code but it doesn't work.

QMenu *menu;
menu =new QMenu();
menu->setStyleSheet("QMenu::icon{height:20px;width:20px});"

I would really appreciate it if someone could provide a solution.

Upvotes: 4

Views: 9038

Answers (3)

Farhad
Farhad

Reputation: 4181

We can set style sheet to manage icon size like this:

QAction *action = new QAction("Exit", this);
action->setIcon(QIcon(":/images/resources/exit.png"));


QMenu *menu = new QMenu("File");
menu->addAction(action);
menu->setStyleSheet("QMenu {icon-size: 200px;} QMenu::item {background: transparent;}");

ui->menubar->addMenu(menu);

screenshot

But it will display in an Improper size, so it's better to use QToolBar.

In your cpp file type this:

ui->ToolBarName->setIconSize(QSize(50,50));

In Designer Click on your QToolbar and set iconSize.

image

Upvotes: 6

ewerybody
ewerybody

Reputation: 1580

Just stumbled across this after all these years. I remember I had this problem once and now again. This time I actually managed to solve it somewhat. It IS kinda weird tho and should receive some love at least documentation-wise.

The key is: You need to style QMenu AND QMenu::item If you just set the icon size via:

QMenu {icon-size: 40px;}

it will remain ignored until you also set something like

QMenu::item {background: transparent;}

Unfortunately this resets the menu stylesheet and you need to do something about the hover state to make it usable. But well. Seems this works for me.

(also posted this on the qt forums)

Upvotes: 1

Veera
Veera

Reputation: 341

Here is the solution that worked for me:

QMenu *menu;
menu =new QMenu();

QToolButton *button=new QToolButton(menu);
button->setFixedSize(50,50);
QWidgetAction *action=new QWidgetAction(this);
action->setDefaultWidget(button);
menu->addAction(action);

Upvotes: 4

Related Questions