April 13, 2011

vinb vinb
Lab Rat
205 posts

[SOLVED] Icon shown in custom menu

 

Hi all,

I have a little problem with getting a icon shown in a QMenu,
i have read the docs, but i dont see what im doing wrong.
I found also a few topics here, but i still didnt solve it.

Here is the code:

  1. void Widget::setupMenu()
  2. {
  3.     //Quit Action, makes the app quit.
  4.     Quit = new QAction(tr("Quit"),this);
  5.     Quit->setIcon(QIcon(":/icon/quit"));
  6.     connect(Quit,SIGNAL(triggered()),this,SLOT(close()));
  7.     //Pause Action, makes the video pause.
  8.     Pause = new QAction(tr("Pause"),this);
  9.     Pause->setIcon(QIcon(":/icon/pause"));
  10.     connect(Pause,SIGNAL(triggered()),obj_MediaObject,SLOT(pause()));
  11.     //Play Action, makes the video play.
  12.     Play = new QAction(tr("Play"),this);
  13.     Play->setIcon(QIcon(":/icon/play"));
  14.     connect(Play,SIGNAL(triggered()),obj_MediaObject,SLOT(play()));
  15.     //Stop Action, makes the video stop.
  16.     Stop = new QAction(tr("Stop"),this);
  17.     Stop->setIcon(QIcon(":/icon/stop"));
  18.     connect(Stop,SIGNAL(triggered()),obj_MediaObject,SLOT(stop()));
  19.  
  20.     videoMenu = new QMenu("VideoMenu",this);
  21.     videoMenu->menuAction()->setIconVisibleInMenu(true);
  22.     videoMenu->addAction(Play);
  23.     videoMenu->menuAction()->setIcon(Play->icon());
  24.     videoMenu->addAction(Pause);
  25.     videoMenu->menuAction()->setIcon(Pause->icon());
  26.     videoMenu->addAction(Stop);
  27.     videoMenu->menuAction()->setIcon(Stop->icon());
  28.     videoMenu->addSeparator();
  29.     videoMenu->addAction(Quit);
  30.     videoMenu->menuAction()->setIcon(Quit->icon());
  31. }

i call the menu with:
  1. void Widget::mousePressEvent(QMouseEvent *event)
  2. {
  3.     if (event->button() == Qt::RightButton)
  4.     {
  5.         QPoint globalPos = wid_videoWidget->mapToGlobal(event->pos());
  6.         videoMenu->exec( globalPos );
  7.     }
  8.     if (event->button() == Qt::LeftButton)
  9.     {
  10.     }
  11. }

The menu is showing but the icons not.
Can somebody help me out to settle this?

Edit:
the path to the icons are ok.
because i allready used the .src file with the tray and window icon.

3 replies

April 13, 2011

Volker Volker
Robot Herder
5428 posts

Seem that your application has the attribute Qt::AA_DontShowIconsInMenus set. Watch if you come over a line like this:

  1. qApp->setAttribute(Qt::AA_DontShowIconsInMenus);

You can try to reset the attribute like this:

  1. qApp->setAttribute(Qt::AA_DontShowIconsInMenus, false);

If this is not applicable for your setup, you must enable the icons on the single actions:

  1. Quit->setIconVisibleInMenu(true);
  2. Play->setIconVisibleInMenu(true);
  3. Pause->setIconVisibleInMenu(true);
  4. Stop->setIconVisibleInMenu(true);

You should leave out the lines like this:

  1. videoMenu->menuAction()->setIcon(Stop->icon());

these just assign an icon of the menu entries to the menu itself.

April 14, 2011

vinb vinb
Lab Rat
205 posts

Hi Volker,
Thanks for your reply.
Your second solution did help solving the problem.
Thanks for your help!!

My working code is now:

  1. void Widget::setupMenu()
  2. {
  3.     //Quit Action, makes the app quit.
  4.     Quit = new QAction(tr("Quit"),this);
  5.     Quit->setIcon(QIcon(":/icon/quit"));
  6.     Quit->setIconVisibleInMenu(true);
  7.     connect(Quit,SIGNAL(triggered()),this,SLOT(close()));
  8.  
  9.     //Pause Action, makes the video pause.
  10.     Pause = new QAction(tr("Pause"),this);
  11.     Pause->setIcon(QIcon(":/icon/pause"));
  12.     Pause->setIconVisibleInMenu(true);
  13.     connect(Pause,SIGNAL(triggered()),obj_MediaObject,SLOT(pause()));
  14.  
  15.     //Play Action, makes the video play.
  16.     Play = new QAction(tr("Play"),this);
  17.     Play->setIcon(QIcon(":/icon/play"));
  18.     Play->setIconVisibleInMenu(true);
  19.     connect(Play,SIGNAL(triggered()),obj_MediaObject,SLOT(play()));
  20.  
  21.     //Stop Action, makes the video stop.
  22.     Stop = new QAction(tr("Stop"),this);
  23.     Stop->setIcon(QIcon(":/icon/stop"));
  24.     Stop->setIconVisibleInMenu(true);
  25.     connect(Stop,SIGNAL(triggered()),obj_MediaObject,SLOT(stop()));
  26.  
  27.     videoMenu = new QMenu("VideoMenu",this);
  28.     videoMenu->addAction(Play);
  29.     videoMenu->addAction(Pause);
  30.     videoMenu->addAction(Stop);
  31.     videoMenu->addSeparator();
  32.     videoMenu->addAction(Quit);
  33.  
  34. }

April 14, 2011

Volker Volker
Robot Herder
5428 posts

Looks perfect!

 
  ‹‹ Context Menus      [SOLVED]Is there a way to unset Qt::FramelessWindowHint? ››

You must log in to post a reply. Not a member yet? Register here!