Details
-
Bug
-
Resolution: Fixed
-
P2: Important
-
5.15.15, 6.2.9, 6.5.2, 6.6.0 Beta3
-
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
Issue Links
- relates to
-
QTBUG-117002 TableModelColumn should use QAbstractItemModel::roleNames()
- Closed