Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qgenericitemmodel.h
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QGENERICITEMMODEL_H
5#define QGENERICITEMMODEL_H
6
7#include <QtCore/qgenericitemmodel_impl.h>
8
10
11class Q_CORE_EXPORT QGenericItemModel : public QAbstractItemModel
12{
13 Q_OBJECT
14public:
15 template <typename T>
16 using SingleColumn = std::tuple<T>;
17
18 template <typename T>
19 struct MultiColumn
20 {
21 using type = std::remove_pointer_t<T>;
22 T data{};
23 template <typename X>
24 using if_get_matches = std::enable_if_t<std::is_same_v<q20::remove_cvref_t<X>,
25 MultiColumn<T>>, bool>;
26
27 template <typename V = T,
28 std::enable_if_t<QGenericItemModelDetails::is_validatable<V>::value, bool> = true>
29 constexpr explicit operator bool() const noexcept { return bool(data); }
30
31 // unconstrained on size_t I, gcc internal error #3280
32 template <std::size_t I, typename V, if_get_matches<V> = true>
33 friend inline decltype(auto) get(V &&multiColumn)
34 {
35 static_assert(I < std::tuple_size_v<type>, "Index out of bounds for wrapped type");
36 return get<I>(QGenericItemModelDetails::refTo(q23::forward_like<V>(multiColumn.data)));
37 }
38 };
39
40 template <typename Range,
41 QGenericItemModelDetails::if_is_table_range<Range> = true>
42 explicit QGenericItemModel(Range &&range, QObject *parent = nullptr);
43
44 template <typename Range,
45 QGenericItemModelDetails::if_is_tree_range<Range> = true>
46 explicit QGenericItemModel(Range &&range, QObject *parent = nullptr);
47
48 template <typename Range, typename Protocol,
49 QGenericItemModelDetails::if_is_tree_range<Range, Protocol> = true>
50 explicit QGenericItemModel(Range &&range, Protocol &&protocol, QObject *parent = nullptr);
51
52 ~QGenericItemModel() override;
53
54 QModelIndex index(int row, int column, const QModelIndex &parent = {}) const override;
55 QModelIndex parent(const QModelIndex &child) const override;
56 QModelIndex sibling(int row, int column, const QModelIndex &index) const override;
57 int rowCount(const QModelIndex &parent = {}) const override;
58 int columnCount(const QModelIndex &parent = {}) const override;
59 Qt::ItemFlags flags(const QModelIndex &index) const override;
60 QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
61 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
62 bool setData(const QModelIndex &index, const QVariant &data, int role = Qt::EditRole) override;
63 QMap<int, QVariant> itemData(const QModelIndex &index) const override;
64 bool setItemData(const QModelIndex &index, const QMap<int, QVariant> &data) override;
65 bool clearItemData(const QModelIndex &index) override;
66 bool insertColumns(int column, int count, const QModelIndex &parent = {}) override;
67 bool removeColumns(int column, int count, const QModelIndex &parent = {}) override;
68 bool moveColumns(const QModelIndex &sourceParent, int sourceColumn, int count,
69 const QModelIndex &destParent, int destColumn) override;
70 bool insertRows(int row, int count, const QModelIndex &parent = {}) override;
71 bool removeRows(int row, int count, const QModelIndex &parent = {}) override;
72 bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count,
73 const QModelIndex &destParent, int destRow) override;
74
75 bool canFetchMore(const QModelIndex &parent) const override;
76 void fetchMore(const QModelIndex &parent) override;
77
78 bool hasChildren(const QModelIndex &parent = QModelIndex()) const override;
79 QModelIndex buddy(const QModelIndex &index) const override;
80 bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
81 const QModelIndex &parent) const override;
82 bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
83 const QModelIndex &parent) override;
84 QMimeData *mimeData(const QModelIndexList &indexes) const override;
85 QStringList mimeTypes() const override;
86 QModelIndexList match(const QModelIndex &start, int role, const QVariant &value, int hits,
87 Qt::MatchFlags flags) const override;
88 void multiData(const QModelIndex &index, QModelRoleDataSpan roleDataSpan) const override;
89 QHash<int, QByteArray> roleNames() const override;
90 void sort(int column, Qt::SortOrder order = Qt::AscendingOrder) override;
91 QSize span(const QModelIndex &index) const override;
92 Qt::DropActions supportedDragActions() const override;
93 Qt::DropActions supportedDropActions() const override;
94
95protected Q_SLOTS:
96 void resetInternalData() override;
97
98protected:
99 bool event(QEvent *) override;
100 bool eventFilter(QObject *, QEvent *) override;
101
102private:
103 Q_DISABLE_COPY_MOVE(QGenericItemModel)
104
105 friend class QGenericItemModelImplBase;
106 struct Deleter { void operator()(QGenericItemModelImplBase *that) { that->destroy(); } };
107 std::unique_ptr<QGenericItemModelImplBase, Deleter> impl;
108};
109
110// implementation of forwarders
111QModelIndex QGenericItemModelImplBase::createIndex(int row, int column, const void *ptr) const
112{
113 return m_itemModel->createIndex(row, column, ptr);
114}
115void QGenericItemModelImplBase::changePersistentIndexList(const QModelIndexList &from,
116 const QModelIndexList &to)
117{
118 m_itemModel->changePersistentIndexList(from, to);
119}
120QHash<int, QByteArray> QGenericItemModelImplBase::roleNames() const
121{
122 return m_itemModel->roleNames();
123}
124void QGenericItemModelImplBase::dataChanged(const QModelIndex &from, const QModelIndex &to,
125 const QList<int> &roles)
126{
127 m_itemModel->dataChanged(from, to, roles);
128}
129void QGenericItemModelImplBase::beginInsertColumns(const QModelIndex &parent, int start, int count)
130{
131 m_itemModel->beginInsertColumns(parent, start, count);
132}
133void QGenericItemModelImplBase::endInsertColumns()
134{
135 m_itemModel->endInsertColumns();
136}
137void QGenericItemModelImplBase::beginRemoveColumns(const QModelIndex &parent, int start, int count)
138{
139 m_itemModel->beginRemoveColumns(parent, start, count);
140}
141void QGenericItemModelImplBase::endRemoveColumns()
142{
143 m_itemModel->endRemoveColumns();
144}
145bool QGenericItemModelImplBase::beginMoveColumns(const QModelIndex &sourceParent, int sourceFirst,
146 int sourceLast, const QModelIndex &destParent,
147 int destColumn)
148{
149 return m_itemModel->beginMoveColumns(sourceParent, sourceFirst, sourceLast,
150 destParent, destColumn);
151}
152void QGenericItemModelImplBase::endMoveColumns()
153{
154 m_itemModel->endMoveColumns();
155}
156
157void QGenericItemModelImplBase::beginInsertRows(const QModelIndex &parent, int start, int count)
158{
159 m_itemModel->beginInsertRows(parent, start, count);
160}
161void QGenericItemModelImplBase::endInsertRows()
162{
163 m_itemModel->endInsertRows();
164}
165void QGenericItemModelImplBase::beginRemoveRows(const QModelIndex &parent, int start, int count)
166{
167 m_itemModel->beginRemoveRows(parent, start, count);
168}
169void QGenericItemModelImplBase::endRemoveRows()
170{
171 m_itemModel->endRemoveRows();
172}
173bool QGenericItemModelImplBase::beginMoveRows(const QModelIndex &sourceParent, int sourceFirst,
174 int sourceLast,
175 const QModelIndex &destParent, int destRow)
176{
177 return m_itemModel->beginMoveRows(sourceParent, sourceFirst, sourceLast, destParent, destRow);
178}
179void QGenericItemModelImplBase::endMoveRows()
180{
181 m_itemModel->endMoveRows();
182}
183QAbstractItemModel &QGenericItemModelImplBase::itemModel()
184{
185 return *m_itemModel;
186}
187const QAbstractItemModel &QGenericItemModelImplBase::itemModel() const
188{
189 return *m_itemModel;
190}
191
192template <typename Range,
193 QGenericItemModelDetails::if_is_table_range<Range>>
194QGenericItemModel::QGenericItemModel(Range &&range, QObject *parent)
195 : QAbstractItemModel(parent)
196 , impl(new QGenericTableItemModelImpl<Range>(std::forward<Range>(range), this))
197{}
198
199template <typename Range,
200 QGenericItemModelDetails::if_is_tree_range<Range>>
201QGenericItemModel::QGenericItemModel(Range &&range, QObject *parent)
202 : QGenericItemModel(std::forward<Range>(range),
203 QGenericItemModelDetails::DefaultTreeProtocol<Range>{}, parent)
204{}
205
206template <typename Range, typename Protocol,
207 QGenericItemModelDetails::if_is_tree_range<Range, Protocol>>
208QGenericItemModel::QGenericItemModel(Range &&range, Protocol &&protocol, QObject *parent)
209 : QAbstractItemModel(parent)
210 , impl(new QGenericTreeItemModelImpl<Range, Protocol>(std::forward<Range>(range),
211 std::forward<Protocol>(protocol), this))
212{}
213
214QT_END_NAMESPACE
215
216namespace std {
217 template <typename T>
221 template <std::size_t I, typename T>
222 struct tuple_element<I, QT_PREPEND_NAMESPACE(QGenericItemModel)::MultiColumn<T>>
223 : tuple_element<I, typename QT_PREPEND_NAMESPACE(QGenericItemModel)::MultiColumn<T>::type>
224 {};
225}
226
227#endif // QGENERICITEMMODEL_H
\inmodule QtCore