Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Icon shown in custom menu
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Icon shown in custom menu

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 14.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vinb
    wrote on 13 Apr 2011, 21:12 last edited by
    #1

    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:
    @
    void Widget::setupMenu()
    {
    //Quit Action, makes the app quit.
    Quit = new QAction(tr("Quit"),this);
    Quit->setIcon(QIcon(":/icon/quit"));
    connect(Quit,SIGNAL(triggered()),this,SLOT(close()));
    //Pause Action, makes the video pause.
    Pause = new QAction(tr("Pause"),this);
    Pause->setIcon(QIcon(":/icon/pause"));
    connect(Pause,SIGNAL(triggered()),obj_MediaObject,SLOT(pause()));
    //Play Action, makes the video play.
    Play = new QAction(tr("Play"),this);
    Play->setIcon(QIcon(":/icon/play"));
    connect(Play,SIGNAL(triggered()),obj_MediaObject,SLOT(play()));
    //Stop Action, makes the video stop.
    Stop = new QAction(tr("Stop"),this);
    Stop->setIcon(QIcon(":/icon/stop"));
    connect(Stop,SIGNAL(triggered()),obj_MediaObject,SLOT(stop()));

    videoMenu = new QMenu("VideoMenu",this);
    videoMenu->menuAction()->setIconVisibleInMenu(true);
    videoMenu->addAction(Play);
    videoMenu->menuAction()->setIcon(Play->icon());
    videoMenu->addAction(Pause);
    videoMenu->menuAction()->setIcon(Pause->icon());
    videoMenu->addAction(Stop);
    videoMenu->menuAction()->setIcon(Stop->icon());
    videoMenu->addSeparator();
    videoMenu->addAction(Quit);
    videoMenu->menuAction()->setIcon(Quit->icon());
    

    }
    @
    i call the menu with:
    @
    void Widget::mousePressEvent(QMouseEvent *event)
    {
    if (event->button() == Qt::RightButton)
    {
    QPoint globalPos = wid_videoWidget->mapToGlobal(event->pos());
    videoMenu->exec( globalPos );
    }
    if (event->button() == Qt::LeftButton)
    {
    }
    }
    @

    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.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on 13 Apr 2011, 21:54 last edited by
      #2

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

      @
      qApp->setAttribute(Qt::AA_DontShowIconsInMenus);
      @

      You can try to reset the attribute like this:

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

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

      @
      Quit->setIconVisibleInMenu(true);
      Play->setIconVisibleInMenu(true);
      Pause->setIconVisibleInMenu(true);
      Stop->setIconVisibleInMenu(true);
      @

      You should leave out the lines like this:

      @
      videoMenu->menuAction()->setIcon(Stop->icon());
      @

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

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vinb
        wrote on 13 Apr 2011, 22:02 last edited by
        #3

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

        My working code is now:
        @
        void Widget::setupMenu()
        {
        //Quit Action, makes the app quit.
        Quit = new QAction(tr("Quit"),this);
        Quit->setIcon(QIcon(":/icon/quit"));
        Quit->setIconVisibleInMenu(true);
        connect(Quit,SIGNAL(triggered()),this,SLOT(close()));

        //Pause Action, makes the video pause.
        Pause = new QAction(tr("Pause"),this);
        Pause->setIcon(QIcon(":/icon/pause"));
        Pause->setIconVisibleInMenu(true);
        connect(Pause,SIGNAL(triggered()),obj_MediaObject,SLOT(pause()));
        
        //Play Action, makes the video play.
        Play = new QAction(tr("Play"),this);
        Play->setIcon(QIcon(":/icon/play"));
        Play->setIconVisibleInMenu(true);
        connect(Play,SIGNAL(triggered()),obj_MediaObject,SLOT(play()));
        
        //Stop Action, makes the video stop.
        Stop = new QAction(tr("Stop"),this);
        Stop->setIcon(QIcon(":/icon/stop"));
        Stop->setIconVisibleInMenu(true);
        connect(Stop,SIGNAL(triggered()),obj_MediaObject,SLOT(stop()));
        
        videoMenu = new QMenu("VideoMenu",this);
        videoMenu->addAction(Play);
        videoMenu->addAction(Pause);
        videoMenu->addAction(Stop);
        videoMenu->addSeparator();
        videoMenu->addAction(Quit);
        

        }
        @

        1 Reply Last reply
        0
        • G Offline
          G Offline
          goetz
          wrote on 13 Apr 2011, 22:06 last edited by
          #4

          Looks perfect!

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • P Offline
            P Offline
            parias
            wrote on 18 Sept 2014, 15:22 last edited by
            #5

            please help since i begun developping in QT i never done it work. still my icon can't be displayed. i tried those solution above but still got problem it not working. im trying to do my browser using network library.
            actionPrecedente = new QAction(QIcon(":/images/precedente"),tr("&Previous"), this);
            actionPrecedente->setIconVisibleInMenu(true);
            you can understand me im french and english speaker

            1 Reply Last reply
            0
            • P Offline
              P Offline
              parias
              wrote on 18 Sept 2014, 15:25 last edited by
              #6

              i ask also in which directory the icon images must be?

              1 Reply Last reply
              0
              • P Offline
                P Offline
                parias
                wrote on 18 Sept 2014, 15:39 last edited by
                #7

                i want it to appear also in toolbar

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved