Image not displaying qt6 c++
-
I'm currently recoding an old QT5 python project in QT6 with c++ and cmake. Everything is working pretty much fine, but when I try displaying images they wont show up.
So I just created a simple test program, with an image, and I can't get it to work. I'm new to using qrc and not quite sure what I'm doing wrong. It shows up in qt designer, but I know that doesn't mean much. It seems like he is not finding the image, but there is no error or feedback in the terminal.
Here is what my project looks like:
Main.qml:
import QtQuick Window { width: 640 height: 480 visible: true color: "#c03434" title: qsTr("Hello World") Rectangle { id: rectangle x: 79 y: 57 width: 483 height: 367 color: "#00ffffff" Image { id: image visible: true anchors.fill: parent source: ":/images/logo_white_100x100.png" fillMode: Image.PreserveAspectFit } } }CMakeList:
cmake_minimum_required(VERSION 3.16) project(biiiigtesterino VERSION 0.1 LANGUAGES CXX) set(CMAKE_AUTOMOC ON) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 6.2 COMPONENTS Quick REQUIRED) qt_add_executable(appbiiiigtesterino main.cpp res.qrc ) qt_add_qml_module(appbiiiigtesterino URI biiiigtesterino VERSION 1.0 QML_FILES main.qml ) set_target_properties(appbiiiigtesterino PROPERTIES MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) target_compile_definitions(appbiiiigtesterino PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>) target_link_libraries(appbiiiigtesterino PRIVATE Qt6::Quick)res.qrc:
<RCC> <qresource prefix="/images"> <file>logo_white_100x100.png</file> <file>main.qml</file> </qresource> </RCC>I've read a lot, but nothing worked yet.
I appreciate any help.
I'm desperate. -
I think you have to tell cmake to call the rcc compiler
according to
https://doc.qt.io/qt-6/resources.html
you have to add
set(CMAKE_AUTORCC ON) -
Hi and welcome to devnet,
What if you use:
source: "images/logo_white_100x100.png"? -
Just for the sake of testing, can you show it on a QLabel ?
-
@SGaist It works when I try to display it on a QLabel, via. a qt widget application and a QPixmap. The code looks like this:
ui->labelImage->setPixmap(QPixmap("D:/Dev/Projects/untitled6/customs.jpg"));I hope I did what you ask me to, because I'm not familiar with QWidgets.
-
@Nykson said in Image not displaying qt6 c++:
ui->labelImage->setPixmap(QPixmap("D:/Dev/Projects/untitled6/customs.jpg"));
Can you repeat this but for the image you were trying to use via QResource?
-
If I do the following steps for qmake and cmake I get two different results:
- create new project
- add Image to main window
- add ressource file to project
(on qmake it gets automatically added to .pro file, but on cmake I add it manually as an executable) - add test image to ressoure file
- change image source to "/test.png".
I used the same image and it was both times in the project folder.
The qmake version runs just fine, but the cmake version says "QML Image: Cannot open: qrc:/test.png".
I tried to add main.qml to the ressource file, but it wont help.
Also if I change "/test.png" to ":/test.png", there is no error, but also no image shown.
I'm really frustrated having this problem, on such a so simple task.
Any help or thoughts are appreciated. -
I think you have to tell cmake to call the rcc compiler
according to
https://doc.qt.io/qt-6/resources.html
you have to add
set(CMAKE_AUTORCC ON)