diff options
| author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2023-03-03 09:29:28 +0100 |
|---|---|---|
| committer | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2023-05-24 09:40:40 +0200 |
| commit | f940f1b87e938fcbcbe4feab0cb49c266558979b (patch) | |
| tree | 9fa2d29b40b10b42a39cfae821acc328bcf7c7a0 /src | |
| parent | 702998e74fc97e6521d70fa9213ccc1a24c66639 (diff) | |
TreeView: add support for setting a root index
This patch will let you set a root index in TreeView.
This can be used to only show a branch of the tree model.
[ChangeLog][Quick][TreeView] You can now set a rootIndex
in TreeView, to only show a branch of the tree model.
Fixes: QTBUG-108541
Change-Id: I1f51d01e0353eeae5d1b08400cb95b306d49f1a3
Reviewed-by: Santhosh Kumar <santhosh.kumar.selvaraj@qt.io>
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src')
| -rw-r--r-- | src/qmlmodels/qqmltreemodeltotablemodel.cpp | 3 | ||||
| -rw-r--r-- | src/quick/items/qquicktreeview.cpp | 31 | ||||
| -rw-r--r-- | src/quick/items/qquicktreeview_p.h | 6 |
3 files changed, 40 insertions, 0 deletions
diff --git a/src/qmlmodels/qqmltreemodeltotablemodel.cpp b/src/qmlmodels/qqmltreemodeltotablemodel.cpp index d300ff2d1a..80f07deaf2 100644 --- a/src/qmlmodels/qqmltreemodeltotablemodel.cpp +++ b/src/qmlmodels/qqmltreemodeltotablemodel.cpp @@ -69,6 +69,9 @@ void QQmlTreeModelToTableModel::setModel(QAbstractItemModel *arg) clearModelData(); m_model = arg; + if (m_rootIndex.isValid() && m_rootIndex.model() != m_model) + m_rootIndex = QModelIndex(); + if (m_model) { for (const Cx *c = &connections[0]; c->signal; c++) connect(m_model, c->signal, this, c->slot); diff --git a/src/quick/items/qquicktreeview.cpp b/src/quick/items/qquicktreeview.cpp index 882487e3a5..521adcadce 100644 --- a/src/quick/items/qquicktreeview.cpp +++ b/src/quick/items/qquicktreeview.cpp @@ -91,6 +91,16 @@ */ /*! + \qmlproperty QModelIndex QtQuick::TreeView::rootIndex + \since 6.6 + + This property holds the model index of the root item in the tree. + By default, this is the same as the root index in the model, but you can + set it to be a child index instead, to show only a branch of the tree. + Set it to \c undefined to show the whole model. +*/ + +/*! \qmlmethod int QtQuick::TreeView::depth(row) Returns the depth (the number of parents up to the root) of the given \a row. @@ -403,6 +413,8 @@ QQuickTreeView::QQuickTreeView(QQuickItem *parent) d->QQuickTableViewPrivate::setModelImpl(modelAsVariant); QObjectPrivate::connect(&d->m_treeModelToTableModel, &QAbstractItemModel::dataChanged, d, &QQuickTreeViewPrivate::dataChangedCallback); + QObject::connect(&d->m_treeModelToTableModel, &QQmlTreeModelToTableModel::rootIndexChanged, + this, &QQuickTreeView::rootIndexChanged); auto tapHandler = new QQuickTapHandler(this); tapHandler->setAcceptedModifiers(Qt::NoModifier); @@ -421,6 +433,25 @@ QQuickTreeView::~QQuickTreeView() { } +QModelIndex QQuickTreeView::rootIndex() const +{ + return d_func()->m_treeModelToTableModel.rootIndex(); +} + +void QQuickTreeView::setRootIndex(const QModelIndex &index) +{ + Q_D(QQuickTreeView); + d->m_treeModelToTableModel.setRootIndex(index); + positionViewAtCell({0, 0}, QQuickTableView::AlignTop | QQuickTableView::AlignLeft); +} + +void QQuickTreeView::resetRootIndex() +{ + Q_D(QQuickTreeView); + d->m_treeModelToTableModel.resetRootIndex(); + positionViewAtCell({0, 0}, QQuickTableView::AlignTop | QQuickTableView::AlignLeft); +} + int QQuickTreeView::depth(int row) const { Q_D(const QQuickTreeView); diff --git a/src/quick/items/qquicktreeview_p.h b/src/quick/items/qquicktreeview_p.h index 3b77477684..5573fd634a 100644 --- a/src/quick/items/qquicktreeview_p.h +++ b/src/quick/items/qquicktreeview_p.h @@ -25,6 +25,7 @@ class QQuickTreeViewPrivate; class Q_QUICK_PRIVATE_EXPORT QQuickTreeView : public QQuickTableView { Q_OBJECT + Q_PROPERTY(QModelIndex rootIndex READ rootIndex WRITE setRootIndex RESET resetRootIndex NOTIFY rootIndexChanged REVISION(6, 6) FINAL) QML_NAMED_ELEMENT(TreeView) QML_ADDED_IN_VERSION(6, 3) @@ -32,6 +33,10 @@ public: QQuickTreeView(QQuickItem *parent = nullptr); ~QQuickTreeView() override; + QModelIndex rootIndex() const; + void setRootIndex(const QModelIndex &index); + void resetRootIndex(); + Q_INVOKABLE int depth(int row) const; Q_INVOKABLE bool isExpanded(int row) const; @@ -54,6 +59,7 @@ public: Q_SIGNALS: void expanded(int row, int depth); void collapsed(int row, bool recursively); + Q_REVISION(6, 6) void rootIndexChanged(); protected: void keyPressEvent(QKeyEvent *event) override; |
