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