[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:
    @
    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.



  • 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.



  • 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);
    

    }
    @



  • Looks perfect!



  • 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



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



  • i want it to appear also in toolbar


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.