Nominate our 2022 Qt Champions!

Models and Views - Best practice

  • A general question regarding models and views, and my simple model-view design:

    Lets say we have a model class containing an assortment of dissimilar data members, (equivalent to a table, with one row, and multiple columns). It has a '***changed()***' signal.

    A custom view has this as its model. It has a slot called update() that connects to the changed() signal.
    The view is made up of an assortment of string edits, and checkboxes.
    The view updates the model if the user changes the contents of any widget.

    The question is, should I always inherit QStandardItemModel / QAbstractItemModel as a starting point for such
    a custom data model? Is this not over engineering? What if the data members are of a complex class type?

    So Is it good enough to just implement a a few change signals, and update the view accordingly?
    What does seem awkward about this simple solution, is that the view's update() method has to disconnect its assortment of widgets from itself, and then reconnect once update() is finished.

    What is best practice?
    What are people's opinion on this?

  • Moderators

    I think QAbstractItemModel and friends is a winner here. It contains a lot of optimized functions, that allow you to update single cells instead of invalidating the view, reconnecting the widgets, etc. It also allows you to leverage all the more advanced mechanisms like lazy initialization (canFetchMore() and fetchMore() methods).

Log in to reply