Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to add png as Icon?



  • I am trying to get a button with an icon show up in my toolbar, so I have the following.

    const QIcon editIcon = QIcon("~/downloads/edit-icon.png");
    
    QAction *editAction = new QAction(editIcon, tr("Edit Vehicle"), this);
    editAction->setStatusTip(tr("Edit Vehicle."));
    vehicleToolbar->addAction(editAction);
    
    connect(editAction, &QAction::triggered, this, &MainWindow::editVehicle);
    

    The button is there (though invisible), and if I click on it, it works, but ... there's no icon on the button.

    Thoughts?



  • @SRaD
    Are you sure if ~/downloads/edit-icon.png is a valid path ?

    If your intention is access the icon by home folder of your system, you can use QDir::homePath()

    #include <QDir>
    
    QDir::homePath() + "/downloads/edit-icon.png"
    

    Note: Files (icons,txt,etc) can be embedded in your application using Qt Resource System.



  • @SRaD
    As @KillerSmath said, the image may not be accessible from the code due to path issue.

    Please check the Qt Resource file system and it is better to use it the contents remains with the project code.

        QAction *editAction = new QAction(QIcon(":/imgAddButtonPressed74x74.png"), tr(""), this);
    
    

    Adding image in Resources qrc file

    RESOURCES += \
        image.qrc
    
    <RCC>
        <qresource prefix="/">
            <file>imgAddButtonPressed74x74.png</file>
        </qresource>
    </RCC>
    


  • Hi @SRaD

    Is the problem resolved ?
    So we can close the thread as solved.


Log in to reply