Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-94505

QAbstractItemModel has no API to add data to the model

    XMLWordPrintable

    Details

    • Type: Suggestion
    • Status: Reported
    • Priority: Not Evaluated
    • Resolution: Unresolved
    • Affects Version/s: 6.2.0 FF
    • Fix Version/s: None
    • Component/s: Core: Item Models
    • Labels:
      None

      Description

      Hi,

      The issue title is a bit generic, but in a nutshell, there are situations inside Qt where a view/view-like class wants to add data to its underlying model.

      QAbstractItemModel has no generic "addData" function.

      Therefore, what view-like classes do is something like:

      1. call insertRow(s) as needed (note that this may fail, because a model may not want rows to be added this way)
      2. call setData() on the newly added rows to populate them (again this may, in theory, fail)

      For instance, consider QComboBox::addItem, which ends up calling this stuff in qcombobox.cpp:

              if (d->model->insertRows(index, 1, d->root)) {
                  QModelIndex item = d->model->index(index, d->modelColumn, d->root);
                  if (icon.isNull() && !userData.isValid()) {
                      d->model->setData(item, text, Qt::EditRole);
      

      This isn't transitional, and if setData fails on the source model then one is left with a dummy row in the model.

      But even without the problem of not being transational, this is simply broken for many other scenarios. For instance consider the case where one has

      Model -> QSFPM -> QComboBox

      The insertRow call succeeds, because QSFPM correctly propagates it to the underlying model. But

      1. if the newly inserted row is filtered out (because, say, it's empty, and QSFPM has a filter to exclude it) then the subsequent calls will return an invalid model index and setData wont' work;
      2. if QSFPM is sorting, this risks overwriting an unrelated index .

      ... QSFPM docs in fact warn about 2) and suggest to disable the dynamic sort filter property. But it's not easy to do when one isn't using QSFPM directly; QComboBox should be in charge of doing it. And even with a non-dynamic QSFPM, 1) still fails.

      In a nutshell, it looks to me that we're missing some API to insert new rows, with data, and return the corresponding QModelIndex(es).

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            dfaure_kdab David Faure
            Reporter:
            peppe Giuseppe D'Angelo
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:

                Gerrit Reviews

                There are no open Gerrit changes