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

TableModel data and setData overloads shadow the QAIM original methods with incompatible types

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 5.15.10, 6.3.1
    • None

    Description

      TableModel has overloads for data and setData to be able to "conveniently" use them from QML.

      Those methods have signatures that make it impossible to use a TableModel as a generic QATM from QML and I'm afraid it's too late to fix now that it has been out in the wild. One possible solution would be to add a compile-time or runtime option.

      Different data and setData in QQmlTableModel, the Q_INVOKABLE one are the ones added ontop of QAIM:

      Q_INVOKABLE QVariant data(const QModelIndex &index, const QString &role) const;
      QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
      Q_INVOKABLE bool setData(const QModelIndex &index, const QString &role, const QVariant &value);
      bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole) override;
      

      I would note that in a TableModel you are most of the times not using roles but columns instead (with the default display role), so those overloads are counterproductive, forcing us to pass an additional parameter.
      And when you are not a roleNumberFromName Q_INVOKABLE would have done the trick fine, removing the need for 2 new Q_INVOKABLE with different signatures.

      The original data() could be made Q_INVOKABLE again (it is in QAIM but the QML engine ignores it since there is a new one) so you could call it with model.data(index, "roleName") or model.data(index, roleNumber), even model.data(index) with the default display role and let the engine find the correct overload.

      The real issue comes from setData().
      The order of the parameters has been changed so you can't make the old one Q_INVOKABLE again since it would become ambiguous.

      Would model.setData(index, "string", int) tries to call setData(QModelIndex index, string roleName, variant value) or setData(QModelIndex index, variant value, int role) ??

      In my QML code I try to call data like that :

      const cellData = model.data(index);

      To workaround this TableModel issue, I now need to do:

      const cellData = model instanceof TableModel ? model.data(index, "display") : model.data(index);
      

      for setData:

      if (model instanceof TableModel)
          model.setData(index, "display", newValue);
      else
          model.setData(index, newValue);
      

      Attachments

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

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            grecko Pierre-Yves Siret
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes