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. Image not displaying qt6 c++

Image not displaying qt6 c++

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 2.7k Views 2 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.
  • N Offline
    N Offline
    Nykson
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Floo
      wrote on last edited by
      #9

      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)

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi and welcome to devnet,

        What if you use: source: "images/logo_white_100x100.png" ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        N 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

          What if you use: source: "images/logo_white_100x100.png" ?

          N Offline
          N Offline
          Nykson
          wrote on last edited by
          #3

          @SGaist
          Still nothing, I tried many different variants and non worked.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Just for the sake of testing, can you show it on a QLabel ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            N 1 Reply Last reply
            0
            • SGaistS SGaist

              Just for the sake of testing, can you show it on a QLabel ?

              N Offline
              N Offline
              Nykson
              wrote on last edited by
              #5

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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mchinand
                wrote on last edited by
                #6

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

                N 1 Reply Last reply
                1
                • M mchinand

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

                  N Offline
                  N Offline
                  Nykson
                  wrote on last edited by
                  #7

                  @mchinand Still works.

                  1 Reply Last reply
                  0
                  • N Offline
                    N Offline
                    Nykson
                    wrote on last edited by Nykson
                    #8

                    If I do the following steps for qmake and cmake I get two different results:

                    1. create new project
                    2. add Image to main window
                    3. add ressource file to project
                      (on qmake it gets automatically added to .pro file, but on cmake I add it manually as an executable)
                    4. add test image to ressoure file
                    5. 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.

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      Floo
                      wrote on last edited by
                      #9

                      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)

                      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