Model/View programming conventions



  • I'm translating the tutorial about model/view programming to my language in order to understand better, the thing is that in the tutorial I found a few name conventions that I'm not acquainted with.
    For example: "item model classes", "item data" and "item views".
    This conventions are used everywhere in the tutorial but I can't find their definition. "item views" is it the same as saying "view's item", like meaning it's a item within a view? I really don't get it.


  • Moderators

    Hi @Volebab,

    You can think of Qt's models as lists (1D arrays), tables (2D arrays), or trees (nested arrays-inside-arrays).

    An "Item" is an element of a list, table, or tree. For example, if your model represents a table with 3 rows and 4 columns, that means your table model contains 12 items.

    • "Item model class" == Any class that inherits from QAbstractItemModel, directly or indirectly. (For example, QAbstractTableModel)
    • "Item data" == The data inside an item. (For example, a number or a string)
    • "Item view" == A view that displays a model's "item data". (For example, QTableView)


  • @JKSH - I understand most of what you explained, the only thing is:

    "Item model class" == Any class that inherits from QAbstractItemModel, directly or indirectly. (For example, QAbstractTableModel)

    Kinda confusing. An item is an element of a model, like a list, table or three, and it has data - "item data" - and can be displayed using, for example, with QTableView, then why "item model class" is classes that inherits from QAbstractItemModel? I mean, "item model" isn't an item inside a model? Why QAbstractTableModel inherits from it?
    When we say that it's inherits something is the same as saying that it's the same, so QAbstractTableModel is an QAbstractItemModel? - What is an "item model"? *mind blown*



  • QAbstractItemView and QAbstractItemModel work in pair.

    If you use Qt based model view approach, any view will be derived from QAbstractItemView , any model will be derived from QAbstractItemModel .
    All QAbstractItemModel defines is API QAbstractItemView will use to communicate with its model.
    Why QAbstractItemModel and not QqAbsrtactViewModel? Probably better name was not found.


  • Moderators

    @Volebab said:

    I mean, "item model" isn't an item inside a model? Why QAbstractTableModel inherits from it?

    No, "item model" is not an item inside a model.

    • "Item model" == A model that contains items.
    • "Item model class" == A C++ class that implements an item model.

    An item is an element of a model, like a list, table or three, and it has data - "item data" - and can be displayed using, for example, with QTableView

    Yes, this is correct. :)

    why "item model class" is classes that inherits from QAbstractItemModel?
    ...
    When we say that it's inherits something is the same as saying that it's the same, so QAbstractTableModel is an QAbstractItemModel? - What is an "item model"? *mind blown*

    Are you familiar with object-oriented programming and inheritance? These are important concepts in C++. You should learn them before you translate a C++ tutorial.

    I guess I should also rewrite my explanation:

    • "Item model class" == QAbstractItemModel, or any other class that inherits from it (directly or indirectly)

    Here are some abstract examples of model inheritance:

    • QAbstractItemModel represents a tree.
    • QAbstractTableModel inherits QAbstractItemModel, and it represents a table. It removes the ability to have nested data.
    • QAbstractListModel inherits QAbstractItemModel, and it represents a list. It removes the ability to have multiple columns.

    Here are some concrete examples of model inheritance:

    • QFileSystemModel inherits QAbstractItemModel, and it represents a folder hierarchy in your computer. Each item represents a file or folder.
    • QSqlQueryModel inherits QAbstractTableModel, and it represents the result of an SQL query. Each item is an element from an SQL SELECT query.
    • QStringListModel inherits QAbstractListModel, and it represents a list of strings. Each item is a string.

    All of these classes are item models.



  • @JKSH - Thank you, helped a lot.


Log in to reply
 

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