Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

Setting QIcon with svg file as a QAction icon problem



  • I have an issue with setting icon for a QAction when image file is an svg image.
    For example:

    QMenu menu("Test");
    QAction* testAction = menu.addAction("Test action");
    testAction->setIcon(QIcon("path_to_svg_file/icon.svg"));
    

    This works fine in qt 4.8.1, but in qt 5.5.1 and 5.6.0rc it does not.
    Tested on Mac OS X 10.10.5 and 10.11.
    Also I have to mention that svg module is surely present in tested qt versions because setting QIcon with an svg image for, for example, QToolButton works good.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Where are you using that menu ?



  • @SGaist thank you for your answer, I've made further investigation and now able to provide you with additional information.

    It seems that behaviour described above actually depends on where QMenu object is used.
    I've tested it with application's menu, tray icon menu and drop-down menu of QToolButton, and results are as follows: icon is visible only in QToolButton's menu.

    Here is a full test example :

    #include <QApplication>
    #include <QMenuBar>
    #include <QToolButton>
    #include <QSystemTrayIcon>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        //create test QMenu
        QMenu* menuPtr = new QMenu("Test menu");
    
        //create test QAction with an svg icon
        QAction* actionPtr = menuPtr->addAction("Test action");
        actionPtr->setIcon(QIcon(":/i/icon.svg"));
        actionPtr->setIconVisibleInMenu(true);
    
        //add test menu to QMenuBar
        QMenuBar *menuBarPtr = new QMenuBar();
        menuBarPtr->addMenu(menuPtr);
    
        QWidget* w = new QWidget();
    
        //add test menu to QToolButton
        QToolButton* toolButtonPtr = new QToolButton(w);
        toolButtonPtr->setText("Test button");
        QObject::connect(toolButtonPtr, SIGNAL(pressed()), toolButtonPtr, SLOT(showMenu()));
        toolButtonPtr->setMenu(menuPtr);
        w->show();
    
        //add test menu to QSystemTrayIcon
        QSystemTrayIcon* trayIconPtr = new QSystemTrayIcon();
        trayIconPtr->setContextMenu(menuPtr);
        trayIconPtr->show();
    
        return a.exec();
    }
    

  • Lifetime Qt Champion

    @rzhurov said:

    actionPtr->setIcon(QIcon(":/i/icon.svg"));

    Replace that by:
    actionPtr->setIcon(QPixmap(":/i/icon.svg"));

    The backend engine used are different when adding a file or a pixmap.



  • @SGaist thank you, it works.
    I suppose it is a bug that should be reported?


  • Lifetime Qt Champion

    I don't know if it's a bug or a missing feature but making it known to the devs is a good idea. You should check the bug report system to see if it's something already known. If not then please open a new report providing a minimal compellable example that shows the behavior.



  • I saw that this bug has been reported.