QML thumbnail list



  • Hi all,

    I have a problem making a thumbnail list/view in QML - I have no idea where to start with this. I've been searching a lot through Qt documentation and stackoverflow, but even though there are some "solutions", they are either not working for my system or not flexible enough. So, the end-effect should be an app similar to Android gallery - once Gallery is loaded, all the images are refreshed. User should also be able to delete images and so on.

    I should point out that there is quite some C++ code in the underlying Qt app, and QML frontend is only constructed via

    [code]
    QQmlApplicationEngine engine(QUrl("qrc:///camera_app.qml"));
    [/code]

    in main.cpp.

    I've created a GridView and statically assigned some images to it and it looks good. The static allocation looks like this:

    [code]
    ListModel {
    id: imageModel
    ListElement {
    name: "Image 01"
    imageUrl: "file:///home/DCIM/image_0000.jpeg"
    }
    ListElement {
    name: "Image 02"
    imageUrl: "file:///home/DCIM/image_0001.jpeg"
    }
    ListElement {
    name: "Image 03"
    imageUrl: "file:///home/DCIM/image_0002.jpeg"
    }
    ListElement {
    name: "Image 04"
    imageUrl: "file:///home/DCIM/image_0003.jpeg"
    }
    }
    [/code]

    However, I'm yet to find a way to dynamically add/remove pictures to/from imageModel. I have tried several ways including trying to do it from C++ with setContextProperty(), but they're not flexible enough, since this requires a pointer to the quickview, which is only available in my main.cpp, not in my underlying c++ code... Not to mention that it doesn't really work, since my ListModel is not in the main QML file. There is also a way to expose C++ model to the QML, but this exposes only model structure, not the list, so I can't really use it (or don't know how to).

    I would really appreciate some help here, as this is the only thing that's been blocking me. In contrast I thought that this would be an easy part... So basically any guidance/advice for some sort of gallery would do just fine. Thanks!



  • Did you look at ListModel insert(...) set(...) etc method. Based on some user action you should be able do something like
    imageModel.insert(....)

    Please note insert expects you insert JSON object. Please look at the above documentation in Assistant. Still if you need help, I can give you code.



  • Hi and thanks for replying. Yesterday I've had some success with append() method - I've realized that it shouldn't be that hard if I use underlying C++ for handling file names only - QML's ListModel append() would work just fine if my underlying C++ class could provide it with proper file names and file count, which is really easy to implement - I'll give it a go and post back.

    What I do find funny is that my whole app blocks when I instantiate the fore-mentioned ListModel without ListElements. My current workaround is to put one ListElement to it and then clear() the list when it's loaded, although this is quite dumb - do you have any idea why ListModel doesn't work when there's no elements in it at startup?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.