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

QAbstractListModel containing a QAbstractListModel for ComboBox QML



  • Hi guys,

    I have a QAbstractListModel (let's call it VideoOptions) which is used by a ListView in QML. Now I want to create a drop down list (let's call it Format) for each element (video) in the VideoOptions model, which seems to be a ComboBox in QML.

    Because of this restriction it seems I can't create a Q_INVOKABLE function in VideoOptions to get the correct QAbstractListModel to use in a ComboBox.

    The basic model layout is the following:

    VideoOptions (ListModel)
       -> VideoOption 1 (Element)
             -> Format (ListModel)
                   -> Format 1 (Element)
                   -> Format 2 (Element)
                   -> Format 3 (Element)
       -> VideoOption 2 (Element)
             -> Format (ListModel)
                   -> Format 1 (Element)
                   -> Format 2 (Element)
                   -> Format 3 (Element)
    

    What is the Qt'iest way to solve this? I currently have a very dirty solution by uncoupling the Format ListModel from the VideoOptions ListModel and creating the exact amount of Format ListModels I need and referencing them by unique names e.g formatListModel1, formatListModel2 in QML.

    Another possibility would be to create the Format ListModel's and share them with the VideoOptions Element by a std::shared_ptr and then create a Q_INVOCABLE function in VideoOptions ListModel to access them from QML.

    In both cases I'd be dependent on an explicit index , which I'd like to avoid because it should be only dependent on the amount of VideoOptions instantiated at one single place.



  • @cirquit Hi, I would create first a ListView to contain all the VideoOption items (In this case, VideoOptions ListModel). The delegate of this ListView would be another element which also has a ListView or a repeater (depending on how much functionality from the listview you need) and set its model to the Format ListModel.
    This way the first ListView displays the VideoOption items, each VideoOption item will have a repeater/ListView to show the format ListModel (this element will be the Combo Item) and then, this item would show each of the format elements.
    I hope I made myself clear.



  • I think I understood your answer, but this does not allow me to create a QAbstractListModel which consists of VideoOptions which each consist of a QAbstractListModel for Format.

    Your proposed solution would be the one I described, as in "I defined a custom model for each VideoOption which I then plug into QML via formatModel1, formatModel2 and formatModel3.

    Please tell me I understood incorrectly :( Is this a new use-case or am I missing something totally obvious? I can't seem to grasp that I'm the only one who wants to group members of a model in a single QAbstractListModel and not have them independent of each other.

    Or maybe is there another way to create a drop-down ComboBox list without using a QAbstractListModel but still be notified of changes and have the model accessable in C++?


  • Qt Champions 2018

    What are the roles of your VideoOptions QALM ?
    Can't you have a role returning your Format QALM?

    It's also kinda hard to help you further since this really depends on your data structure and on how you store your data underneath the model.


Log in to reply