Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Image in Rectangle does not show



  • I tryed annother code from an example. But it does not work.

    Following code:

    import QtQuick 2.14
    
    Rectangle {
        id: root
        width: background.width; height: background.height
    
        Image {
            id: background
            source: "assets/background_medium.png"
        }
    }
    

    When I run this I get a window which appears in the top left corner of my screen with size = 0. I can drag it bigger but nothing shows.

    I don't get the usuall message that the image could not be loaded so It should be ok.

    Why is it not working?



  • @sandro4912 said in Image in Rectangle does not show:

    width: background.width; height: background.height

    It sounds crazy but now It works.

    What I did. I changed the code to this:

    Rectangle {
        id: root
    
        Image {
            id: background
            source: "assets/background_medium.png"
        }
    
        width: background.width; height: background.height
    }
    

    Then run it. And it worked.

    Afterwards I changed this back to the original

    Rectangle {
        id: root
        width: background.width; height: background.height
    
        Image {
            id: background
            source: "assets/background_medium.png"
        }
    }
    

    And now this also works.


Log in to reply