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

dynamically load images during runtime



  • How do I dynamically load images during runtime.
    Example: Album Art, Contact Pictures, etc.

    Currently I'm using array kind of implementation but it really requires the images to be embedded inside the executable. But I found it difficult when any new addition/deletion to the music/contact database are done.

    My C++ code reads the filename and pass the file name to the QML and it also places the file inside the "pics" folder. But still I couldn't able to see the images unless & until I load the images using qml.qrc->resource editor

    I get the below error if I don't add the file physically.
    QML Image: Cannot open: qrc:/qml/pics/Bluetooth.png


  • Lifetime Qt Champion

    Hi,

    Your question is not quite clear. Do you mean add new pictures at run time that were not available when you built your application ?



  • @Vyuvaraj What is the name format which C++ sends to Qml? What is it like? You may use imageprovider to send images directly to Qml



  • @SGaist YES. Currently I have hardcoded the file names and included in my project. I wanted to make it dynamic, so that I reduce the number of hardcoded file names and file into the project. I'm expecting something like "looking to a specific folder where the exe is placed and using the required picture(s) during runtime".


  • Lifetime Qt Champion

    How would you determine the name of the files to use in your application ?



  • @Yunus .png, .jpeg, .jpg files



  • Hi, you can add file:// in front of the path of your files in order to load images outside qrc.
    i.e. file:///home/media/pic1.jpg or so.



  • @SGaist I use a template like the below
    <AlbumDetails Name = "Album 1" Art = "Album1.jpg">
    <Song Name = "Song 1" Artist = "Artist 1"/>
    <Song Name = "Song 2" Artist = "Artist 2"/>
    <Song Name = "Song 3" Artist = "Artist 1, Artist 2"/>
    </AlbumDetails>

    I will create a list of Songs under each Album details which also contains Album Art jpg file name.


  • Lifetime Qt Champion

    As @rrlopez already wrote, use the file:// scheme and you can access the files.


Log in to reply