Important: Please read the Qt Code of Conduct -

ListModel and GridView

  • I have a model with two QStringList Q_PROPERTY's one is a list of image locations the other is a list of strings. I want to try and display this data in a GridView but I am not sure if I should try to use the data to populate a ListModel or if I should just feed it one of the lists as a the model and envoke the other list with an index. Can I dynamically create ListElement's like that?

  • Create a proper list model in C++ that has everything in one place.

  • @fcarney I would try to pipe from this model to a proper QAbstractListModel but that seems like a whole lot of work for something that's only going to be used once and in a limited manner.

  • Dear @Circuits ,

    Why you want to use two separate list (like QList) and why you don't just create one QList from your own template class for QList like QList<MyData> which includes your titles and paths of images,icons etc. In this case, I guess you can manage things far easier than before.

    I use QAbstractListModel and it works fine without any special limitation! Could you please offer more information?


  • Here is "a whole lot of work":

    • File->New File or Project->Qt->Qt Item Model
    • Press Choose
    • In Base Class: select QAbstractListModel
    • Name Class
    • Choose options
    • Choose next
    • Choose finish

    Do have to define roles and such, but Qt Creator does the boiler plate crap fairly well.

  • This post is deleted!

  • @fcarney Well wouldn't I have to implement an insertRows(), beginInsertRows(), endInsertRows()? The same for columns, or any other method I wish to use? Could I somehow pipe the information from these lists I have already created over too this new class? As of now they are part of a model called settings:

    class SettingV: public QObject
    Q_PROPERTY(QStringList optionsList      READ optionsList    WRITE setOptionsList    NOTIFY optionsListChanged)
    Q_PROPERTY(QStringList optionsListImages      READ optionsListImages    WRITE setOptionsListImages    NOTIFY optionsListImagesChanged)

    That being said,, the reason I just created a new list is because I already have this ListView component that is displaying the "optionsList" items in a list and I figured it should be easy to just convert that component into a GridView and index off of another list for the images that will be attached to the "optionsList" strings ie the logic is simple:

    for each item in "options List"
    set Image source: property to the corresponding string in the other list

    Since that will be the image url of the image associated with that item because of how I created the lists.. using enums..

Log in to reply