diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2023-09-13 17:02:35 +0800 |
---|---|---|
committer | Qt Cherry-pick Bot <cherrypick_bot@qt-project.org> | 2023-09-20 00:40:25 +0000 |
commit | fc2afde37d5d7a00bf26cbcbd3e7622a285b8bdd (patch) | |
tree | adf9a0bc23fe15037894acc8a6da03ec321922a9 | |
parent | 39755a5e65642403dde3bdf5b63dab8b23d98874 (diff) |
HeaderView: fix custom role names not being used
QHeaderDataProxyModel did not implement roleNames(), so
QAbstractItemModel::roleNames was being used. That function returns
the default role names, ignoring any custom ones.
Fixes: QTBUG-116748
Change-Id: Iff7e0d7c71c60da7ace54aee5857605e8bea8d0c
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
(cherry picked from commit 1fe24b6658c0835ed9fe0bc82f62312f468ec67d)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 2eeccf8909bdf9c51f14c09d75ec524e7bb4189a)
4 files changed, 87 insertions, 0 deletions
diff --git a/src/quicktemplates/qquickheaderview.cpp b/src/quicktemplates/qquickheaderview.cpp index 7544a5651c..7396e6da59 100644 --- a/src/quicktemplates/qquickheaderview.cpp +++ b/src/quicktemplates/qquickheaderview.cpp @@ -304,6 +304,11 @@ bool QHeaderDataProxyModel::hasChildren(const QModelIndex &parent) const return false; } +QHash<int, QByteArray> QHeaderDataProxyModel::roleNames() const +{ + return m_model ? m_model->roleNames() : QAbstractItemModel::roleNames(); +} + QVariant QHeaderDataProxyModel::variantValue() const { return QVariant::fromValue(static_cast<QObject *>(const_cast<QHeaderDataProxyModel *>(this))); diff --git a/src/quicktemplates/qquickheaderview_p_p.h b/src/quicktemplates/qquickheaderview_p_p.h index fe33cb0190..6b772dbab8 100644 --- a/src/quicktemplates/qquickheaderview_p_p.h +++ b/src/quicktemplates/qquickheaderview_p_p.h @@ -44,6 +44,7 @@ public: QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; bool hasChildren(const QModelIndex &parent = QModelIndex()) const override; + QHash<int, QByteArray> roleNames() const override; inline QVariant variantValue() const; inline Qt::Orientation orientation() const; diff --git a/tests/auto/quickcontrols/qquickheaderview/data/headerData.qml b/tests/auto/quickcontrols/qquickheaderview/data/headerData.qml new file mode 100644 index 0000000000..914913f409 --- /dev/null +++ b/tests/auto/quickcontrols/qquickheaderview/data/headerData.qml @@ -0,0 +1,37 @@ +// Copyright (C) 2023 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause + +import QtQuick +import QtQuick.Controls +import TestTableModelWithHeader + +ApplicationWindow { + width: 400 + height: 400 + + property alias headerView: headerView + + Column { + HorizontalHeaderView { + id: headerView + syncView: tableView + textRole: "customRole" + } + TableView { + id: tableView + width: 100 + height: 100 + model: TestTableModelWithHeader { + rowCount: 2 + columnCount: 2 + } + delegate: Label { + text: customRole + leftPadding: 10 + rightPadding: 10 + + required property string customRole + } + } + } +} diff --git a/tests/auto/quickcontrols/qquickheaderview/tst_qquickheaderview.cpp b/tests/auto/quickcontrols/qquickheaderview/tst_qquickheaderview.cpp index 346e45f6d4..019736c7d7 100644 --- a/tests/auto/quickcontrols/qquickheaderview/tst_qquickheaderview.cpp +++ b/tests/auto/quickcontrols/qquickheaderview/tst_qquickheaderview.cpp @@ -15,6 +15,7 @@ #include <QtQuickTestUtils/private/visualtestutils_p.h> #include <QtQuickTemplates2/private/qquickapplicationwindow_p.h> #include <QtQuickTemplates2/private/qquickheaderview_p.h> +#include <QtQuickTemplates2/private/qquicklabel_p.h> #include <private/qquickheaderview_p_p.h> using namespace QQuickVisualTestUtils; @@ -134,6 +135,10 @@ class TestTableModelWithHeader : public TestTableModel { Q_OBJECT public: + enum Role { + CustomRole = Qt::UserRole + }; + void setRowCount(int count) override { vData.resize(count); @@ -145,6 +150,17 @@ public: hData.resize(count); TestTableModel::setColumnCount(count); } + + QVariant data(const QModelIndex &index, int role) const override + { + switch (role) { + case CustomRole: + return QString("%1-%2").arg(index.column()).arg(index.row()); + default: + return TestTableModel::data(index, role); + } + } + Q_INVOKABLE QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override { @@ -157,6 +173,8 @@ public: auto &data = orientation == Qt::Horizontal ? hData : vData; return data[section].toString(); } + case CustomRole: + return (orientation == Qt::Horizontal ? "c" : "r") + QString::number(section); default: return QVariant(); } @@ -178,6 +196,13 @@ public: return true; } + Q_INVOKABLE QHash<int, QByteArray> roleNames() const override + { + auto names = TestTableModel::roleNames(); + names[CustomRole] = "customRole"; + return names; + } + private: QList<QVariant> hData, vData; }; @@ -202,6 +227,8 @@ private slots: void resizableHandlerBlockingEvents(); + void headerData(); + private: QQmlEngine *engine; QString errorString; @@ -401,6 +428,23 @@ void tst_QQuickHeaderView::resizableHandlerBlockingEvents() QVERIFY(mouseArea->pressed()); } +void tst_QQuickHeaderView::headerData() +{ + QQuickApplicationHelper helper(this, QStringLiteral("headerData.qml")); + QVERIFY2(helper.errorMessage.isEmpty(), helper.errorMessage); + QQuickWindow *window = helper.window; + window->show(); + QVERIFY(QTest::qWaitForWindowExposed(window)); + + auto headerView = window->property("headerView").value<QQuickHeaderViewBase *>(); + QVERIFY(headerView); + const auto firstHeaderCell = headerView->itemAtIndex(headerView->index(0, 0)); + QVERIFY(firstHeaderCell); + const auto label = firstHeaderCell->findChild<QQuickLabel *>(); + QVERIFY(label); + QCOMPARE(label->text(), "c0"); +} + QTEST_MAIN(tst_QQuickHeaderView) #include "tst_qquickheaderview.moc" |