4

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.

Veera
  • 331
  • 3
  • 15

3 Answers3

5

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

Farhad
  • 4,119
  • 8
  • 43
  • 66
  • @HiFile.app-bestfilemanager Absolutely agree. Thought I misread the post when I saw this was the accepted answer. Flagged. – Edward Severinsen Dec 25 '22 at 18:50
  • You're right my friend, I updated my answer and explained it more, thanks for your reply. @EdwardSeverinsen – Farhad Dec 27 '22 at 17:14
4

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);
Veera
  • 331
  • 3
  • 15
1

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)

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
ewerybody
  • 1,443
  • 16
  • 29
  • icon-size is not applicable for QMenu. Please check the documentation: https://doc.qt.io/qt-6/stylesheet-reference.html – Erhan Dec 26 '22 at 19:02