[solved] QPushButton and setIcon doesn't work - icon is not showing up



  • (Visual 2012, Qt 5.2.1 - add-in)

    Hi,
    I have my class Button that inherits QPushButton. I create, using that class, a lot of buttons and I want to set icon to that buttons.
    Firstly, I wrote:

    button.cpp

    @
    Button::Button(const QString& name))
    {
    setText(name);
    setLayoutDirection(Qt::RightToLeft);
    QIcon buttonIcon(":Resources/btn.png");
    setIcon(buttonIcon);
    }
    @

    But that code doesn't work. Something is setting to that buttons because text is moving to the left but there is nothing visible (if I comment code that is responsible for setting icon, text is align to center).
    Any idea how to fix it?



  • :Resources/btn.png is a wrong path.
    What is in your resource file?



  • Like qxoz said, you most likely forgot the '/' before "Resources".



  • hmm.. but it still doesn't work.
    I tried couple of combinations with the same .png file and I add it to the resource.

    • I copied image to the resource folder in my project and I use relative path. - not working
    • I copied image to the resource folder in my project and I use full path - not working
    • I copied image on the desktop and I use full path - works.

    First method may not working because I have folders names with space (ie. "c:\users\me\documents\visual studio 2012\projects\project1...")
    Third method works, but I can't do it in that way.
    Second method - I have no idea why it doesn't work



  • Do you have a resource file (.qrc)?
    "Qt Resource System":http://qt-project.org/doc/qt-5/resources.html



  • Oh, sorry, my mistake. I didn't noticed that I have only .rc file (created by VS while creting project) not .qrc. Now it works.


Log in to reply
 

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