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
qabstractitemview.h
Go to the documentation of this file.
1// Copyright (C) 2016 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// Qt-Security score:significant reason:default
4
5#ifndef QABSTRACTITEMVIEW_H
6#define QABSTRACTITEMVIEW_H
7
8#include <QtWidgets/qtwidgetsglobal.h>
9#include <QtWidgets/qabstractscrollarea.h>
10#include <QtCore/qabstractitemmodel.h>
11#include <QtCore/qitemselectionmodel.h>
12#include <QtWidgets/qabstractitemdelegate.h>
13
14class tst_QAbstractItemView;
15class tst_QTreeView;
16
18
19QT_BEGIN_NAMESPACE
20
21class QMenu;
22class QDrag;
23class QEvent;
24class QAbstractItemViewPrivate;
25
26class Q_WIDGETS_EXPORT QAbstractItemView : public QAbstractScrollArea
27{
28 Q_OBJECT
29 Q_PROPERTY(bool autoScroll READ hasAutoScroll WRITE setAutoScroll)
30 Q_PROPERTY(int autoScrollMargin READ autoScrollMargin WRITE setAutoScrollMargin)
31 Q_PROPERTY(EditTriggers editTriggers READ editTriggers WRITE setEditTriggers)
32 Q_PROPERTY(bool tabKeyNavigation READ tabKeyNavigation WRITE setTabKeyNavigation)
33#if QT_CONFIG(draganddrop)
34 Q_PROPERTY(bool showDropIndicator READ showDropIndicator WRITE setDropIndicatorShown)
35 Q_PROPERTY(bool dragEnabled READ dragEnabled WRITE setDragEnabled)
36 Q_PROPERTY(bool dragDropOverwriteMode READ dragDropOverwriteMode WRITE setDragDropOverwriteMode)
37 Q_PROPERTY(DragDropMode dragDropMode READ dragDropMode WRITE setDragDropMode)
38 Q_PROPERTY(Qt::DropAction defaultDropAction READ defaultDropAction WRITE setDefaultDropAction)
39#endif
40 Q_PROPERTY(bool alternatingRowColors READ alternatingRowColors WRITE setAlternatingRowColors)
41 Q_PROPERTY(SelectionMode selectionMode READ selectionMode WRITE setSelectionMode)
42 Q_PROPERTY(SelectionBehavior selectionBehavior READ selectionBehavior
43 WRITE setSelectionBehavior)
44 Q_PROPERTY(QSize iconSize READ iconSize WRITE setIconSize NOTIFY iconSizeChanged)
45 Q_PROPERTY(Qt::TextElideMode textElideMode READ textElideMode WRITE setTextElideMode)
46 Q_PROPERTY(ScrollMode verticalScrollMode READ verticalScrollMode WRITE setVerticalScrollMode
47 RESET resetVerticalScrollMode)
48 Q_PROPERTY(ScrollMode horizontalScrollMode READ horizontalScrollMode
49 WRITE setHorizontalScrollMode RESET resetHorizontalScrollMode)
50 Q_PROPERTY(int updateThreshold READ updateThreshold WRITE setUpdateThreshold)
51
52public:
53 enum SelectionMode {
54 NoSelection,
55 SingleSelection,
56 MultiSelection,
57 ExtendedSelection,
58 ContiguousSelection
59 };
60 Q_ENUM(SelectionMode)
61
62 enum SelectionBehavior {
63 SelectItems,
64 SelectRows,
65 SelectColumns
66 };
67 Q_ENUM(SelectionBehavior)
68
69 enum ScrollHint {
70 EnsureVisible,
71 PositionAtTop,
72 PositionAtBottom,
73 PositionAtCenter
74 };
75 Q_ENUM(ScrollHint)
76
77 enum EditTrigger {
78 NoEditTriggers = 0,
79 CurrentChanged = 1,
80 DoubleClicked = 2,
81 SelectedClicked = 4,
82 EditKeyPressed = 8,
83 AnyKeyPressed = 16,
84 AllEditTriggers = 31
85 };
86
87 Q_DECLARE_FLAGS(EditTriggers, EditTrigger)
88 Q_FLAG(EditTriggers)
89
90 enum ScrollMode {
91 ScrollPerItem,
92 ScrollPerPixel
93 };
94 Q_ENUM(ScrollMode)
95
96 explicit QAbstractItemView(QWidget *parent = nullptr);
97 ~QAbstractItemView();
98
99 virtual void setModel(QAbstractItemModel *model);
100 QAbstractItemModel *model() const;
101
102 virtual void setSelectionModel(QItemSelectionModel *selectionModel);
103 QItemSelectionModel *selectionModel() const;
104
105 void setItemDelegate(QAbstractItemDelegate *delegate);
106 QAbstractItemDelegate *itemDelegate() const;
107
108 void setSelectionMode(QAbstractItemView::SelectionMode mode);
109 QAbstractItemView::SelectionMode selectionMode() const;
110
111 void setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior);
112 QAbstractItemView::SelectionBehavior selectionBehavior() const;
113
114 QModelIndex currentIndex() const;
115 QModelIndex rootIndex() const;
116
117 void setEditTriggers(EditTriggers triggers);
118 EditTriggers editTriggers() const;
119
120 void setVerticalScrollMode(ScrollMode mode);
121 ScrollMode verticalScrollMode() const;
122 void resetVerticalScrollMode();
123
124 void setHorizontalScrollMode(ScrollMode mode);
125 ScrollMode horizontalScrollMode() const;
126 void resetHorizontalScrollMode();
127
128 void setAutoScroll(bool enable);
129 bool hasAutoScroll() const;
130
131 void setAutoScrollMargin(int margin);
132 int autoScrollMargin() const;
133
134 void setTabKeyNavigation(bool enable);
135 bool tabKeyNavigation() const;
136
137#if QT_CONFIG(draganddrop)
138 void setDropIndicatorShown(bool enable);
139 bool showDropIndicator() const;
140
141 void setDragEnabled(bool enable);
142 bool dragEnabled() const;
143
144 void setDragDropOverwriteMode(bool overwrite);
145 bool dragDropOverwriteMode() const;
146
147 enum DragDropMode {
148 NoDragDrop,
149 DragOnly,
150 DropOnly,
151 DragDrop,
152 InternalMove
153 };
154 Q_ENUM(DragDropMode)
155
156 void setDragDropMode(DragDropMode behavior);
157 DragDropMode dragDropMode() const;
158
159 void setDefaultDropAction(Qt::DropAction dropAction);
160 Qt::DropAction defaultDropAction() const;
161#endif
162
163 void setAlternatingRowColors(bool enable);
164 bool alternatingRowColors() const;
165
166 void setIconSize(const QSize &size);
167 QSize iconSize() const;
168
169 void setTextElideMode(Qt::TextElideMode mode);
170 Qt::TextElideMode textElideMode() const;
171
172 virtual void keyboardSearch(const QString &search);
173
174 virtual QRect visualRect(const QModelIndex &index) const = 0;
175 virtual void scrollTo(const QModelIndex &index, ScrollHint hint = EnsureVisible) = 0;
176 virtual QModelIndex indexAt(const QPoint &point) const = 0;
177
178 QSize sizeHintForIndex(const QModelIndex &index) const;
179 virtual int sizeHintForRow(int row) const;
180 virtual int sizeHintForColumn(int column) const;
181
182 int updateThreshold() const;
183 void setUpdateThreshold(int threshold);
184
185 void openPersistentEditor(const QModelIndex &index);
186 void closePersistentEditor(const QModelIndex &index);
187 bool isPersistentEditorOpen(const QModelIndex &index) const;
188
189 void setIndexWidget(const QModelIndex &index, QWidget *widget);
190 QWidget *indexWidget(const QModelIndex &index) const;
191
192 void setItemDelegateForRow(int row, QAbstractItemDelegate *delegate);
193 QAbstractItemDelegate *itemDelegateForRow(int row) const;
194
195 void setItemDelegateForColumn(int column, QAbstractItemDelegate *delegate);
196 QAbstractItemDelegate *itemDelegateForColumn(int column) const;
197
198#if QT_DEPRECATED_SINCE(6, 0)
199 QT_DEPRECATED_VERSION_X_6_0("Use itemDelegateForIndex instead")
200 QAbstractItemDelegate *itemDelegate(const QModelIndex &index) const
201 { return itemDelegateForIndex(index); }
202#endif
203 virtual QAbstractItemDelegate *itemDelegateForIndex(const QModelIndex &index) const;
204
205 virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const override;
206
207 using QAbstractScrollArea::update;
208
209public Q_SLOTS:
210 virtual void reset();
211 virtual void setRootIndex(const QModelIndex &index);
212 virtual void doItemsLayout();
213 virtual void selectAll();
214 void edit(const QModelIndex &index);
215 void clearSelection();
216 void setCurrentIndex(const QModelIndex &index);
217 void scrollToTop();
218 void scrollToBottom();
219 void update(const QModelIndex &index);
220
221protected Q_SLOTS:
222 virtual void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight,
223 const QList<int> &roles = QList<int>());
224 virtual void rowsInserted(const QModelIndex &parent, int start, int end);
225 virtual void rowsAboutToBeRemoved(const QModelIndex &parent, int start, int end);
226 virtual void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
227 virtual void currentChanged(const QModelIndex &current, const QModelIndex &previous);
228 virtual void updateEditorData();
229 virtual void updateEditorGeometries();
230 virtual void updateGeometries();
231 virtual void verticalScrollbarAction(int action);
232 virtual void horizontalScrollbarAction(int action);
233 virtual void verticalScrollbarValueChanged(int value);
234 virtual void horizontalScrollbarValueChanged(int value);
235 virtual void closeEditor(QWidget *editor, QAbstractItemDelegate::EndEditHint hint);
236 virtual void commitData(QWidget *editor);
237 virtual void editorDestroyed(QObject *editor);
238
239Q_SIGNALS:
240 void pressed(const QModelIndex &index);
241 void clicked(const QModelIndex &index);
242 void doubleClicked(const QModelIndex &index);
243
244 void activated(const QModelIndex &index);
245 void entered(const QModelIndex &index);
246 void viewportEntered();
247
248 void iconSizeChanged(const QSize &size);
249
250protected:
251 QAbstractItemView(QAbstractItemViewPrivate &, QWidget *parent = nullptr);
252
253 enum CursorAction { MoveUp, MoveDown, MoveLeft, MoveRight,
254 MoveHome, MoveEnd, MovePageUp, MovePageDown,
255 MoveNext, MovePrevious };
256 virtual QModelIndex moveCursor(CursorAction cursorAction,
257 Qt::KeyboardModifiers modifiers) = 0;
258
259 virtual int horizontalOffset() const = 0;
260 virtual int verticalOffset() const = 0;
261
262 virtual bool isIndexHidden(const QModelIndex &index) const = 0;
263
264 virtual void setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command) = 0;
265 virtual QRegion visualRegionForSelection(const QItemSelection &selection) const = 0;
266 virtual QModelIndexList selectedIndexes() const;
267
268 virtual bool edit(const QModelIndex &index, EditTrigger trigger, QEvent *event);
269
270 virtual QItemSelectionModel::SelectionFlags selectionCommand(const QModelIndex &index,
271 const QEvent *event = nullptr) const;
272
273#if QT_CONFIG(draganddrop)
274 virtual void startDrag(Qt::DropActions supportedActions);
275#endif
276
277 virtual void initViewItemOption(QStyleOptionViewItem *option) const;
278
279 enum State {
280 NoState,
281 DraggingState,
282 DragSelectingState,
283 EditingState,
284 ExpandingState,
285 CollapsingState,
286 AnimatingState
287 };
288
289 State state() const;
290 void setState(State state);
291
292 void scheduleDelayedItemsLayout();
293 void executeDelayedItemsLayout();
294
295 void setDirtyRegion(const QRegion &region);
296 void scrollDirtyRegion(int dx, int dy);
297 QPoint dirtyRegionOffset() const;
298
299 void startAutoScroll();
300 void stopAutoScroll();
301 void doAutoScroll();
302
303 bool focusNextPrevChild(bool next) override;
304 bool event(QEvent *event) override;
305 bool viewportEvent(QEvent *event) override;
306 void mousePressEvent(QMouseEvent *event) override;
307 void mouseMoveEvent(QMouseEvent *event) override;
308 void mouseReleaseEvent(QMouseEvent *event) override;
309 void mouseDoubleClickEvent(QMouseEvent *event) override;
310#if QT_CONFIG(draganddrop)
311 void dragEnterEvent(QDragEnterEvent *event) override;
312 void dragMoveEvent(QDragMoveEvent *event) override;
313 void dragLeaveEvent(QDragLeaveEvent *event) override;
314 void dropEvent(QDropEvent *event) override;
315#endif
316 void focusInEvent(QFocusEvent *event) override;
317 void focusOutEvent(QFocusEvent *event) override;
318 void keyPressEvent(QKeyEvent *event) override;
319 void resizeEvent(QResizeEvent *event) override;
320 void timerEvent(QTimerEvent *event) override;
321 void inputMethodEvent(QInputMethodEvent *event) override;
322 bool eventFilter(QObject *object, QEvent *event) override;
323
324#if QT_CONFIG(draganddrop)
325 enum DropIndicatorPosition { OnItem, AboveItem, BelowItem, OnViewport };
326 DropIndicatorPosition dropIndicatorPosition() const;
327#endif
328
329 QSize viewportSizeHint() const override;
330
331private:
332 Q_DECLARE_PRIVATE(QAbstractItemView)
333 Q_DISABLE_COPY(QAbstractItemView)
334
335 friend class ::tst_QAbstractItemView;
336 friend class ::tst_QTreeView;
337 friend class QTreeViewPrivate; // needed to compile with MSVC
338 friend class QListModeViewBase;
339 friend class QListViewPrivate;
340 friend class QAbstractSlider;
341 friend class QComboBoxPrivate; // needed to call initViewItemOption
342};
343
344Q_DECLARE_OPERATORS_FOR_FLAGS(QAbstractItemView::EditTriggers)
345
346QT_END_NAMESPACE
347
348#endif // QABSTRACTITEMVIEW_H
The QAbstractItemDelegate class is used to display and edit data items from a model.
The QAbstractItemView class provides the basic functionality for item view classes.
\inmodule QtCore
QT_REQUIRE_CONFIG(itemviews)
static bool editorHandlesKeyEvent(QWidget *editor, const QKeyEvent *event)