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

Questions about models, items and views



  • I want to display some data that is tabular in form. There is no hierarchy. The model can be the data store

    I want to have a check box and Icon in the first column of the data, and I want to be able to set attributes of a row by their name rather than by index. QStandardItemModel and QStandardItem appear to do a lot of what I want (as I am sure they are intended to):

    1. Can I use QStandardItemModel/QStandardItem in a way that allows me to access the elements of each row by name, if so how can I do that?

    2. If not is there a model that I can readily subclass that will allow access by column name and also allows use of an Icon and check box. If so how should I proceed to do that? Assume that the underlying data will be a structure/class of QStrings, or possible QStrings and integers. Say for the sake of argument that it has three members of type QString called File, Type and Score.

    3. Will a QTableView work to display the model? The final display should look like:

    66b63299-59db-4006-b93b-7bafe8c68022-image.png

    Thanks
    David



  • What do you mean "by name"

    I mean I want to be able to access item.File, item.Type, item.Score rather than only having access by an index into an array of QStrings



  • @Perdrix said in Questions about models, items and views:

    the elements of each row by name

    What do you mean "by name"?

    Will a QTableView work to display the model?

    Yes



  • What do you mean "by name"

    I mean I want to be able to access item.File, item.Type, item.Score rather than only having access by an index into an array of QStrings



  • what you normally do is set an enumerator to determine the columns:

    enum ModelColumns{
    mcFile
    , mcType
    , mcScore
    };
    

    then you use model->item(row,mcFile) to access it using QStandardItemModel (or generically, to read/write model->index(row,mcFile).data()/model->setData(model->index(row,mcFile),QStringLiteral("Something")))



  • OK! That will get the job done!

    Thanks - it's not knowing the idioms that always catches you out.


Log in to reply