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

QIcon not working when making an 'empty qmake project'



  • I was trying out this tutorial on Qt Creator 4.1.0. I made an 'Empty qmake project' (I assumed that was the same as 'Empty Qt Project' in the tutorial), and I was able to get a QApplication and QPushButton working, but I can't get a QIcon to show for the button. I made a 'resource.qrc' and added a 'web.png' to it, no prefix, and I expected something like 'QIcon(":/web.png")' to work. What's odd is that if I made a 'Qt Widgets Application' project, commented out '#include "mainwindow.h"', and tried the same thing, adding a 'resource.qrc' and 'web.png', then adding a QIcon to a QPushButton worked. The image was showing.
    Is anyone able to get a QIcon to show after making an 'empty qmake project'? When I tried it, I was on Windows 8.1, and I used only the kit 'Desktop Qt 5.7.0 MSVC2015_64bit'. I thought since my .pro,main.cpp, and resource.qrc file were small, I 'd include them here.

    /TEMPLATE = app
    TARGET = name_of_the_app
    
    QT = core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    SOURCES += \
        main.cpp
    
    RESOURCES += \
        resource.qrc
    
    
    #include <QApplication>
    #include <QPushButton>
    
    int main(int argc, char **argv)
    {
     QApplication app (argc, argv);
    
     QPushButton button ("Hello world !");
     QIcon icon(":/web.png");\
     button.setIcon(icon);
     button.show();
    
     return app.exec();
    }
    
    
    <RCC>
        <qresource prefix="/">
            <file>web.png</file>
        </qresource>
    </RCC>

  • Lifetime Qt Champion

    Hi
    it looks ok syntax wise.
    Did you run qmake after adding the qres?



  • Hi

    @DragonautX

    Welcome to the Qt Forum

    I just tried the same thing Using Other Project->Empty qmake Project.
    I am using Qt version 5.7, Windows OS 8.1.

    In my .pro file

    /TEMPLATE = app
    TARGET = name_of_the_app

    QT = core gui

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    SOURCES +=
    main.cpp

    RESOURCES +=
    otherfiles.qrc

    main.cpp file

    #include <QApplication>
    #include <QPushButton>

    int main(int argc, char **argv)
    {
    QApplication app (argc, argv);

    QPushButton button;
    QSize size(200,200);
    QIcon icon(":/images.png");
    button.setIcon(icon);
    button.setIconSize(size);
    button.show();
    
    
    app.exec();
    
    return app.exec();
    

    }

    I used QSize to increase the size of image in QPushbutton.
    I added resource file , without prefix.

    <RCC>
    <qresource prefix="/">
    <file>images.png</file>
    </qresource>
    </RCC>

    I have attached the output.

    https://postimg.org/image/c4n0n3795/

    Can u delete pro.user file in folder,
    clean the project, run qmake and build and run to see the result.

    Thanks,



  • @mrjj Wow, that worked, thank you. Why does it work like that? When I was trying out this, I'd always do Ctrl + R to Run. Is running qmake different than clicking Run?



  • @Pradeep-Kumar I tried your idea after @mrjj , and that worked too. I'm realizing now there's three steps I can do, run qmake, build, and run. Do you know how is running qmake different from either building or running? I thought in c++ programs you first build and then run second, and then nothing more.



  • Hi,

    qmake is required to generate project files, where the information provided in .pro files, later use build and run.

    U can go through qmake tutoial

    http://doc.qt.io/qt-4.8/qmake-manual.html

    Thanks,



  • I hope ur question is solved , if so please mark your topic as solved , which will be useful in future to the users.

    Cheers,
    Thanks,


Log in to reply