Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QIcon not working when making an 'empty qmake project'
QtWS25 Last Chance

QIcon not working when making an 'empty qmake project'

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DragonautX
    wrote on last edited by
    #1

    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>
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      D 1 Reply Last reply
      2
      • Pradeep KumarP Offline
        Pradeep KumarP Offline
        Pradeep Kumar
        wrote on last edited by Pradeep Kumar
        #3

        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,

        Pradeep Kumar
        Qt,QML Developer

        D 1 Reply Last reply
        3
        • mrjjM mrjj

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

          D Offline
          D Offline
          DragonautX
          wrote on last edited by
          #4

          @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?

          1 Reply Last reply
          1
          • Pradeep KumarP Pradeep Kumar

            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,

            D Offline
            D Offline
            DragonautX
            wrote on last edited by
            #5

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

            1 Reply Last reply
            1
            • Pradeep KumarP Offline
              Pradeep KumarP Offline
              Pradeep Kumar
              wrote on last edited by Pradeep Kumar
              #6

              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,

              Pradeep Kumar
              Qt,QML Developer

              1 Reply Last reply
              4
              • Pradeep KumarP Offline
                Pradeep KumarP Offline
                Pradeep Kumar
                wrote on last edited by Pradeep Kumar
                #7

                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,

                Pradeep Kumar
                Qt,QML Developer

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved