I can't display a png image in a label
-
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 -
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)
@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)
-
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@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. -
@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.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
-
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
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(); } -
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(); }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)
-
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)
@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)
-
@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)
@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)


However, thanks for help :)
-
B beautifulcloud has marked this topic as solved on