Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. I can't display a png image in a label
Qt 6.12 LTS is out! See what's new in the release blog

I can't display a png image in a label

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.9k Views 1 Watching
  • 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.
  • beautifulcloudB Offline
    beautifulcloudB Offline
    beautifulcloud
    wrote on last edited by
    #1

    Hello,

    I know very little about QT Creator, I tried to display an image in a label and I'm wrong somewhere.

    I created a label with text in mainwindow.ui and it shows at runtime. But when I try to display an image on this same label, neither the text nor the image is displayed, there is just a white screen. Here is my mainwindow.cpp simple code:

    #include "mainwindow.h"
    #include "./ui_mainwindow.h"
    
    #include <QLabel>
    #include <QPixmap>
    
    
    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        QPixmap pm(":/assets/background.png"); // <- path to image file
        ui->label->setPixmap(pm);
        ui->label->setScaledContents(true);
    
        ui->label->show();
    }
    
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

    There is what I did :

    • I added in CMakeLists.txt :
    file(COPY assets DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
    
    • And I also noticed what appears to be an error in "6. General Messages":
    Running C:\Qt\Tools\CMake_64\bin\cmake.exe -S F:/Prog/C/MyProg -B F:/Prog/C/build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug in F:\Prog\C\build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug.
    -- Could NOT find WrapVulkanHeaders (missing: Vulkan_INCLUDE_DIR) 
    CMake Error at CMakeLists.txt:82 (file):
      file COPY cannot find "F:/Prog/C/MyProg/images": No error.
    
    
    -- Configuring incomplete, errors occurred!
    See also "F:/Prog/C/build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug/CMakeFiles/CMakeOutput.log".
    CMake process exited with exit code 1.
    
    Elapsed time: 00:01.
    
    Running C:\Qt\Tools\CMake_64\bin\cmake.exe -S F:/Prog/C/MyProg -B F:/Prog/C/build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug in F:\Prog\C\build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug.
    -- Could NOT find WrapVulkanHeaders (missing: Vulkan_INCLUDE_DIR) 
    -- Configuring done
    -- Generating done
    -- Build files have been written to: F:/Prog/C/build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug
    Elapsed time: 00:04.
    
    Running C:\Qt\Tools\CMake_64\bin\cmake.exe -S F:/Prog/C/MyProg -B F:/Prog/C/build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug in F:\Prog\C\build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug.
    -- Could NOT find WrapVulkanHeaders (missing: Vulkan_INCLUDE_DIR) 
    -- Configuring done
    CMake Error at CMakeLists.txt:62 (target_link_libraries):
      Target "MyProg" links to:
    
        Qt6::Multimedia
    
      but the target was not found.  Possible reasons include:
    
        * There is a typo in the target name.
        * A find_package call is missing for an IMPORTED target.
        * An ALIAS target is missing.
    
    
    
    -- Generating done
    CMake Generate step failed.  Build files cannot be regenerated correctly.
    CMake process exited with exit code 1.
    
    Elapsed time: 00:01.
    
    Running C:\Qt\Tools\CMake_64\bin\cmake.exe -S F:/Prog/C/MyProg -B F:/Prog/C/build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug in F:\Prog\C\build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug.
    -- Could NOT find WrapVulkanHeaders (missing: Vulkan_INCLUDE_DIR) 
    -- Configuring done
    -- Generating done
    CMake Error:
      Running
    
       'C:/msys64/mingw32/bin/ninja.exe' '-C' 'F:/Prog/C/build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug' '-t' 'recompact'
    
      failed with:
    
       ninja: error: failed recompaction: Permission denied
    
    
    
    CMake Error:
      Running
    
       'C:/msys64/mingw32/bin/ninja.exe' '-C' 'F:/Prog/C/build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug' '-t' 'restat' 'build.ninja'
    
      failed with:
    
       ninja: error: failed recompaction: Permission denied
    
    
    
    CMake Generate step failed.  Build files cannot be regenerated correctly.
    CMake process exited with exit code 1.
    
    Elapsed time: 00:01.
    

    I don't know if it's important,

    If someone has an idea why I can't place this image, I tested another image and it didn't work
    Thank you

    Christian EhrlicherC 1 Reply Last reply
    0
    • beautifulcloudB beautifulcloud

      Hello @Abderrahmene_Rayene

      I did exactly the same then I noticed that the label is empty, it's only working with the absolute path.

      (I noticed because the text on the label disapear when I try to display the image)

      C Offline
      C Offline
      CPPUIX
      wrote on last edited by CPPUIX
      #6

      @beautifulcloud said in I can't display a png image in a label:

      I noticed because the text on the label disapear when I try to display the image

      That would probably be because you used the wrong relative path to your image, or made a typo. I tried with a typo in my path and got an empty label, text disappeared. I'm ruling out the possibility that your image does not exist because you said you can use it with an absolute path.

      Could you share the exact code you used, and your project's structure? (folders and files)

      beautifulcloudB 1 Reply Last reply
      1
      • beautifulcloudB beautifulcloud

        Hello,

        I know very little about QT Creator, I tried to display an image in a label and I'm wrong somewhere.

        I created a label with text in mainwindow.ui and it shows at runtime. But when I try to display an image on this same label, neither the text nor the image is displayed, there is just a white screen. Here is my mainwindow.cpp simple code:

        #include "mainwindow.h"
        #include "./ui_mainwindow.h"
        
        #include <QLabel>
        #include <QPixmap>
        
        
        MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
            QPixmap pm(":/assets/background.png"); // <- path to image file
            ui->label->setPixmap(pm);
            ui->label->setScaledContents(true);
        
            ui->label->show();
        }
        
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        

        There is what I did :

        • I added in CMakeLists.txt :
        file(COPY assets DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
        
        • And I also noticed what appears to be an error in "6. General Messages":
        Running C:\Qt\Tools\CMake_64\bin\cmake.exe -S F:/Prog/C/MyProg -B F:/Prog/C/build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug in F:\Prog\C\build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug.
        -- Could NOT find WrapVulkanHeaders (missing: Vulkan_INCLUDE_DIR) 
        CMake Error at CMakeLists.txt:82 (file):
          file COPY cannot find "F:/Prog/C/MyProg/images": No error.
        
        
        -- Configuring incomplete, errors occurred!
        See also "F:/Prog/C/build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug/CMakeFiles/CMakeOutput.log".
        CMake process exited with exit code 1.
        
        Elapsed time: 00:01.
        
        Running C:\Qt\Tools\CMake_64\bin\cmake.exe -S F:/Prog/C/MyProg -B F:/Prog/C/build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug in F:\Prog\C\build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug.
        -- Could NOT find WrapVulkanHeaders (missing: Vulkan_INCLUDE_DIR) 
        -- Configuring done
        -- Generating done
        -- Build files have been written to: F:/Prog/C/build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug
        Elapsed time: 00:04.
        
        Running C:\Qt\Tools\CMake_64\bin\cmake.exe -S F:/Prog/C/MyProg -B F:/Prog/C/build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug in F:\Prog\C\build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug.
        -- Could NOT find WrapVulkanHeaders (missing: Vulkan_INCLUDE_DIR) 
        -- Configuring done
        CMake Error at CMakeLists.txt:62 (target_link_libraries):
          Target "MyProg" links to:
        
            Qt6::Multimedia
        
          but the target was not found.  Possible reasons include:
        
            * There is a typo in the target name.
            * A find_package call is missing for an IMPORTED target.
            * An ALIAS target is missing.
        
        
        
        -- Generating done
        CMake Generate step failed.  Build files cannot be regenerated correctly.
        CMake process exited with exit code 1.
        
        Elapsed time: 00:01.
        
        Running C:\Qt\Tools\CMake_64\bin\cmake.exe -S F:/Prog/C/MyProg -B F:/Prog/C/build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug in F:\Prog\C\build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug.
        -- Could NOT find WrapVulkanHeaders (missing: Vulkan_INCLUDE_DIR) 
        -- Configuring done
        -- Generating done
        CMake Error:
          Running
        
           'C:/msys64/mingw32/bin/ninja.exe' '-C' 'F:/Prog/C/build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug' '-t' 'recompact'
        
          failed with:
        
           ninja: error: failed recompaction: Permission denied
        
        
        
        CMake Error:
          Running
        
           'C:/msys64/mingw32/bin/ninja.exe' '-C' 'F:/Prog/C/build-MyProg-Desktop_Qt_6_5_1_MinGW_64_bit-Debug' '-t' 'restat' 'build.ninja'
        
          failed with:
        
           ninja: error: failed recompaction: Permission denied
        
        
        
        CMake Generate step failed.  Build files cannot be regenerated correctly.
        CMake process exited with exit code 1.
        
        Elapsed time: 00:01.
        

        I don't know if it's important,

        If someone has an idea why I can't place this image, I tested another image and it didn't work
        Thank you

        Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @beautifulcloud said in I can't display a png image in a label:

        QPixmap pm(":/assets/background.png");

        file(COPY assets DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

        Why do you copy a file when you reference a path in a resource file?
        Please read https://doc.qt.io/qt-6/resources.html to see how to use Qt resources and add files to them.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        beautifulcloudB 1 Reply Last reply
        2
        • Christian EhrlicherC Christian Ehrlicher

          @beautifulcloud said in I can't display a png image in a label:

          QPixmap pm(":/assets/background.png");

          file(COPY assets DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

          Why do you copy a file when you reference a path in a resource file?
          Please read https://doc.qt.io/qt-6/resources.html to see how to use Qt resources and add files to them.

          beautifulcloudB Offline
          beautifulcloudB Offline
          beautifulcloud
          wrote on last edited by beautifulcloud
          #3

          Hello @Christian-Ehrlicher ,

          Thanks for your answer, I don't know QT very well and I deleted that copy line, I guess these are probably ridiculous problems.

          According to your documentation, I created a .qrc file named ressources :

          <RCC>
            <qresource>
              <file>assets/background.png</file>
            </qresource>
          </RCC>
          

          then in CMake I add :

          set(CMAKE_AUTORCC ON) # this
          
          set(PROJECT_SOURCES
                  main.cpp
                  mainwindow.cpp
                  mainwindow.h
                  mainwindow.ui
          
                  ressources.qrc # this
          )
          

          I still can't import the image, is it due to the errors above or I'm wrong in the configuration?

          Thanks for your time

          EDIT : I managed to import the image directly with an absolute path :

              QPixmap pm("F:/Prog/C/MyProg/assets/background.png"); // <- path to image file
              ui->label->setPixmap(pm);
          

          I don't understand the use of the .qrc file or how to import an image with a relative path, if someone can answer me that would be nice thanks

          C 1 Reply Last reply
          0
          • beautifulcloudB beautifulcloud

            Hello @Christian-Ehrlicher ,

            Thanks for your answer, I don't know QT very well and I deleted that copy line, I guess these are probably ridiculous problems.

            According to your documentation, I created a .qrc file named ressources :

            <RCC>
              <qresource>
                <file>assets/background.png</file>
              </qresource>
            </RCC>
            

            then in CMake I add :

            set(CMAKE_AUTORCC ON) # this
            
            set(PROJECT_SOURCES
                    main.cpp
                    mainwindow.cpp
                    mainwindow.h
                    mainwindow.ui
            
                    ressources.qrc # this
            )
            

            I still can't import the image, is it due to the errors above or I'm wrong in the configuration?

            Thanks for your time

            EDIT : I managed to import the image directly with an absolute path :

                QPixmap pm("F:/Prog/C/MyProg/assets/background.png"); // <- path to image file
                ui->label->setPixmap(pm);
            

            I don't understand the use of the .qrc file or how to import an image with a relative path, if someone can answer me that would be nice thanks

            C Offline
            C Offline
            CPPUIX
            wrote on last edited by
            #4

            Hi,

            @beautifulcloud said in I can't display a png image in a label:

            I deleted that copy line

            Keep it deleted, try to work out the resource system, it is very useful.

            @beautifulcloud said in I can't display a png image in a label:

            According to your documentation, I created a .qrc file named ressources :

            <RCC>
              <qresource>
                <file>assets/background.png</file>
              </qresource>
            </RCC>
            

            then in CMake I add :

            set(CMAKE_AUTORCC ON) # this
            
            set(PROJECT_SOURCES
                    main.cpp
                    mainwindow.cpp
                    mainwindow.h
                    mainwindow.ui
            
                    ressources.qrc # this
            )
            

            This looks correct.

            @beautifulcloud said in I can't display a png image in a label:

            I still can't import the image

            Could you specify what that means exactly? Do you get an empty QLabel? Do you get errors? Does your program run?

            What happens if you try a minimal example? Something like this:

            #include "mainwindow.h"
            #include <QApplication>
            #include <QPixmap>
            #include <QLabel>
            
            int main(int argc,char*argv[])
            {
                QApplication a(argc, argv);
            
                //MainWindow w;
                //w.show();
            
                QLabel label;
            
                label.setPixmap(QPixmap(":/assets/background.png"));
            
                label.show();
            
                return a.exec();
            }
            
            beautifulcloudB 1 Reply Last reply
            1
            • C CPPUIX

              Hi,

              @beautifulcloud said in I can't display a png image in a label:

              I deleted that copy line

              Keep it deleted, try to work out the resource system, it is very useful.

              @beautifulcloud said in I can't display a png image in a label:

              According to your documentation, I created a .qrc file named ressources :

              <RCC>
                <qresource>
                  <file>assets/background.png</file>
                </qresource>
              </RCC>
              

              then in CMake I add :

              set(CMAKE_AUTORCC ON) # this
              
              set(PROJECT_SOURCES
                      main.cpp
                      mainwindow.cpp
                      mainwindow.h
                      mainwindow.ui
              
                      ressources.qrc # this
              )
              

              This looks correct.

              @beautifulcloud said in I can't display a png image in a label:

              I still can't import the image

              Could you specify what that means exactly? Do you get an empty QLabel? Do you get errors? Does your program run?

              What happens if you try a minimal example? Something like this:

              #include "mainwindow.h"
              #include <QApplication>
              #include <QPixmap>
              #include <QLabel>
              
              int main(int argc,char*argv[])
              {
                  QApplication a(argc, argv);
              
                  //MainWindow w;
                  //w.show();
              
                  QLabel label;
              
                  label.setPixmap(QPixmap(":/assets/background.png"));
              
                  label.show();
              
                  return a.exec();
              }
              
              beautifulcloudB Offline
              beautifulcloudB Offline
              beautifulcloud
              wrote on last edited by
              #5

              Hello @Abderrahmene_Rayene

              I did exactly the same then I noticed that the label is empty, it's only working with the absolute path.

              (I noticed because the text on the label disapear when I try to display the image)

              C 1 Reply Last reply
              0
              • beautifulcloudB beautifulcloud

                Hello @Abderrahmene_Rayene

                I did exactly the same then I noticed that the label is empty, it's only working with the absolute path.

                (I noticed because the text on the label disapear when I try to display the image)

                C Offline
                C Offline
                CPPUIX
                wrote on last edited by CPPUIX
                #6

                @beautifulcloud said in I can't display a png image in a label:

                I noticed because the text on the label disapear when I try to display the image

                That would probably be because you used the wrong relative path to your image, or made a typo. I tried with a typo in my path and got an empty label, text disappeared. I'm ruling out the possibility that your image does not exist because you said you can use it with an absolute path.

                Could you share the exact code you used, and your project's structure? (folders and files)

                beautifulcloudB 1 Reply Last reply
                1
                • C CPPUIX

                  @beautifulcloud said in I can't display a png image in a label:

                  I noticed because the text on the label disapear when I try to display the image

                  That would probably be because you used the wrong relative path to your image, or made a typo. I tried with a typo in my path and got an empty label, text disappeared. I'm ruling out the possibility that your image does not exist because you said you can use it with an absolute path.

                  Could you share the exact code you used, and your project's structure? (folders and files)

                  beautifulcloudB Offline
                  beautifulcloudB Offline
                  beautifulcloud
                  wrote on last edited by
                  #7

                  @Abderrahmene_Rayene

                  I think I'll stick with your absolute paths, I just wanted to know if it was obvious why the relative path didn't work.

                  I got a project folder with my CMake, source, headers, qrc files then I got another folder into it named "assets" where the image background.png was stored and I didn't change the configuration above (despite I tested another methods)
                  2c4452f0-309f-422e-a0a6-19f36c681090-image.png
                  9b07f7ba-0bd7-4b97-ba98-bea3dc9100cf-image.png

                  However, thanks for help :)

                  1 Reply Last reply
                  0
                  • beautifulcloudB beautifulcloud has marked this topic as solved on

                  • Login

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