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

HorizontalHeaderView does not use QAbstractItemModel::roleNames()

    XMLWordPrintable

Details

    • 1fe24b665 (dev), 2eeccf890 (6.6), fc2afde37 (6.5)

    Description

      https://doc.qt.io/qt-6/qml-qtquick-controls2-horizontalheaderview.html#textRole-prop contains a link to the roleNames() function, but HorizontalHeaderView does not seem to call that function or use its results.

       

      Code

      // mytablemodel.h
      class MyTableModel : public QAbstractTableModel
      {
          Q_OBJECT
          QML_ELEMENT
      
      public:
          enum ExtraRoles {
              ShortDisplayRole = Qt::UserRole+1
          };
      
          explicit MyTableModel(QObject *parent = nullptr) : QAbstractTableModel(parent) {}
          int rowCount(const QModelIndex &parent = QModelIndex()) const override { return parent.isValid() ? 0 : 2; }
          int columnCount(const QModelIndex &parent = QModelIndex()) const override{ return parent.isValid() ? 0 : 2; }
      
          QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override
          {
              if (role == Qt::DisplayRole)
                  return (orientation == Qt::Horizontal ? "Col" : "Row") + QString::number(section);
              else if (role == ShortDisplayRole)
                  return (orientation == Qt::Horizontal ? "c" : "r") + QString::number(section);
              return QVariant();
          }
          QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
          {
              if (role == Qt::DisplayRole)
                  return QString("(%1, %2)").arg(index.column()).arg(index.row());
              else if (role == ShortDisplayRole)
                  return QString("%1-%2").arg(index.column()).arg(index.row());
              return QVariant();
          }
      
          QHash<int, QByteArray> roleNames() const override
          {
              auto roles = QAbstractItemModel::roleNames();
              roles[ShortDisplayRole] = "shortDisplay";
              return roles;
          }
      };
      
      // main.qml
      import QtQuick 2.15
      import QtQuick.Window 2.15
      import QtQuick.Controls 2.15
      import HeaderUserRolesTest 1.0
      
      Window {
          width: 400
          height: 300
          visible: true
          color: "grey"
      
          MyTableModel { id: myTableModel }
      
          Column {
              HorizontalHeaderView {
                  syncView: regularView
                  textRole: "display"
              }
              TableView {
                  id: regularView
                  width: 100
                  height: 100
                  model: myTableModel
                  delegate: Label {
                      required property string display
                      text: display
                      padding: 5
                  }
              }
      
              HorizontalHeaderView {
                  syncView: compactView
                  textRole: "shortDisplay"
              }
              TableView {
                  id: compactView
                  width: 100
                  height: 100
                  model: myTableModel
                  delegate: Label {
                      required property string shortDisplay
                      text: shortDisplay
                      padding: 5
                  }
              }
          }
      }
      

       

      Outcomes

      • regularView and its headers are displayed as expected
      • compactView is displayed as expected
      • compactView's headers are displayed as blanks (not expected)
      • The console output shows "HorizontalHeaderView.qml: Unable to assign [undefined] to QString" (not expected)

       

      Workaround
      Hijack one of the existing default roles, instead of using a custom role (for example, textRole: "toolTip")

      Attachments

        1. qtbug116748.zip
          5 kB
          Mitch Curtis

        Issue Links

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

          Activity

            People

              mitch_curtis Mitch Curtis
              skoh-qt Sze Howe Koh
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes