QAbstractItemView Class

The QAbstractItemView class provides the basic functionality for item view classes. More...

Header: #include <QAbstractItemView>
qmake: QT += widgets
Inherits: QAbstractScrollArea
Inherited By:

QColumnView, QHeaderView, QListView, QTableView, and QTreeView

Public Types

enum DragDropMode { NoDragDrop, DragOnly, DropOnly, DragDrop, InternalMove }
enum EditTrigger { NoEditTriggers, CurrentChanged, DoubleClicked, SelectedClicked, EditKeyPressed, …, AllEditTriggers }
enum ScrollHint { EnsureVisible, PositionAtTop, PositionAtBottom, PositionAtCenter }
enum ScrollMode { ScrollPerItem, ScrollPerPixel }
enum SelectionBehavior { SelectItems, SelectRows, SelectColumns }
enum SelectionMode { SingleSelection, ContiguousSelection, ExtendedSelection, MultiSelection, NoSelection }

Properties

Public Functions

bool alternatingRowColors() const
int autoScrollMargin() const
Qt::DropAction defaultDropAction() const
QAbstractItemView::DragDropMode dragDropMode() const
bool dragDropOverwriteMode() const
bool dragEnabled() const
QAbstractItemView::EditTriggers editTriggers() const
bool hasAutoScroll() const
QAbstractItemView::ScrollMode horizontalScrollMode() const
QSize iconSize() const
void resetHorizontalScrollMode()
void resetVerticalScrollMode()
QAbstractItemView::SelectionBehavior selectionBehavior() const
QAbstractItemView::SelectionMode selectionMode() const
void setAlternatingRowColors(bool enable)
void setAutoScroll(bool enable)
void setAutoScrollMargin(int margin)
void setDefaultDropAction(Qt::DropAction dropAction)
void setDragDropMode(QAbstractItemView::DragDropMode behavior)
void setDragDropOverwriteMode(bool overwrite)
void setDragEnabled(bool enable)
void setDropIndicatorShown(bool enable)
void setEditTriggers(QAbstractItemView::EditTriggers triggers)
void setHorizontalScrollMode(QAbstractItemView::ScrollMode mode)
void setIconSize(const QSize &size)
void setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)
void setSelectionMode(QAbstractItemView::SelectionMode mode)
void setTabKeyNavigation(bool enable)
void setTextElideMode(Qt::TextElideMode mode)
void setVerticalScrollMode(QAbstractItemView::ScrollMode mode)
bool showDropIndicator() const
bool tabKeyNavigation() const
Qt::TextElideMode textElideMode() const
QAbstractItemView::ScrollMode verticalScrollMode() const

Signals

void iconSizeChanged(const QSize &size)

Protected Types

enum CursorAction { MoveUp, MoveDown, MoveLeft, MoveRight, MoveHome, …, MovePrevious }
enum DropIndicatorPosition { OnItem, AboveItem, BelowItem, OnViewport }
enum State { NoState, DraggingState, DragSelectingState, EditingState, ExpandingState, …, AnimatingState }

Detailed Description

QAbstractItemView class is the base class for every standard view that uses a QAbstractItemModel. QAbstractItemView is an abstract class and cannot itself be instantiated. It provides a standard interface for interoperating with models through the signals and slots mechanism, enabling subclasses to be kept up-to-date with changes to their models. This class provides standard support for keyboard and mouse navigation, viewport scrolling, item editing, and selections. The keyboard navigation implements this functionality:

KeysFunctionality
Arrow keysChanges the current item and selects it.
Ctrl+Arrow keysChanges the current item but does not select it.
Shift+Arrow keysChanges the current item and selects it. The previously selected item(s) is not deselected.
Ctr+SpaceToggles selection of the current item.
Tab/BacktabChanges the current item to the next/previous item.
Home/EndSelects the first/last item in the model.
Page up/Page downScrolls the rows shown up/down by the number of visible rows in the view.
Ctrl+ASelects all items in the model.

Note that the above table assumes that the selection mode allows the operations. For instance, you cannot select items if the selection mode is QAbstractItemView::NoSelection.

The QAbstractItemView class is one of the Model/View Classes and is part of Qt's model/view framework.

The view classes that inherit QAbstractItemView only need to implement their own view-specific functionality, such as drawing items, returning the geometry of items, finding items, etc.

QAbstractItemView provides common slots such as edit() and setCurrentIndex(). Many protected slots are also provided, including dataChanged(), rowsInserted(), rowsAboutToBeRemoved(), selectionChanged(), and currentChanged().

The root item is returned by rootIndex(), and the current item by currentIndex(). To make sure that an item is visible use scrollTo().

Some of QAbstractItemView's functions are concerned with scrolling, for example setHorizontalScrollMode() and setVerticalScrollMode(). To set the range of the scroll bars, you can, for example, reimplement the view's resizeEvent() function:

 void MyView::resizeEvent(QResizeEvent *event) {
     horizontalScrollBar()->setRange(0, realWidth - width());
     ...
 }

Note that the range is not updated until the widget is shown.

Several other functions are concerned with selection control; for example setSelectionMode(), and setSelectionBehavior(). This class provides a default selection model to work with (selectionModel()), but this can be replaced by using setSelectionModel() with an instance of QItemSelectionModel.

For complete control over the display and editing of items you can specify a delegate with setItemDelegate().

QAbstractItemView provides a lot of protected functions. Some are concerned with editing, for example, edit(), and commitData(), whilst others are keyboard and mouse event handlers.

Note: If you inherit QAbstractItemView and intend to update the contents of the viewport, you should use viewport->update() instead of update() as all painting operations take place on the viewport.

See also View Classes, Model/View Programming, QAbstractItemModel, and Chart Example.

Member Type Documentation

enum QAbstractItemView::CursorAction

This enum describes the different ways to navigate between items,

ConstantValueDescription
QAbstractItemView::MoveUp0Move to the item above the current item.
QAbstractItemView::MoveDown1Move to the item below the current item.
QAbstractItemView::MoveLeft2Move to the item left of the current item.
QAbstractItemView::MoveRight3Move to the item right of the current item.
QAbstractItemView::MoveHome4Move to the top-left corner item.
QAbstractItemView::MoveEnd5Move to the bottom-right corner item.
QAbstractItemView::MovePageUp6Move one page up above the current item.
QAbstractItemView::MovePageDown7Move one page down below the current item.
QAbstractItemView::MoveNext8Move to the item after the current item.
QAbstractItemView::MovePrevious9Move to the item before the current item.

See also moveCursor().

enum QAbstractItemView::DragDropMode

Describes the various drag and drop events the view can act upon. By default the view does not support dragging or dropping (NoDragDrop).

ConstantValueDescription
QAbstractItemView::NoDragDrop0Does not support dragging or dropping.
QAbstractItemView::DragOnly1The view supports dragging of its own items
QAbstractItemView::DropOnly2The view accepts drops
QAbstractItemView::DragDrop3The view supports both dragging and dropping
QAbstractItemView::InternalMove4The view accepts move (not copy) operations only from itself.

Note that the model used needs to provide support for drag and drop operations.

This enum was introduced or modified in Qt 4.2.

See also setDragDropMode() and Using drag and drop with item views.

enum QAbstractItemView::DropIndicatorPosition

This enum indicates the position of the drop indicator in relation to the index at the current mouse position:

ConstantValueDescription
QAbstractItemView::OnItem0The item will be dropped on the index.
QAbstractItemView::AboveItem1The item will be dropped above the index.
QAbstractItemView::BelowItem2The item will be dropped below the index.
QAbstractItemView::OnViewport3The item will be dropped onto a region of the viewport with no items. The way each view handles items dropped onto the viewport depends on the behavior of the underlying model in use.

enum QAbstractItemView::EditTrigger

This enum describes actions which will initiate item editing.

ConstantValueDescription
QAbstractItemView::NoEditTriggers0No editing possible.
QAbstractItemView::CurrentChanged1Editing start whenever current item changes.
QAbstractItemView::DoubleClicked2Editing starts when an item is double clicked.
QAbstractItemView::SelectedClicked4Editing starts when clicking on an already selected item.
QAbstractItemView::EditKeyPressed8Editing starts when the platform edit key has been pressed over an item.
QAbstractItemView::AnyKeyPressed16Editing starts when any key is pressed over an item.
QAbstractItemView::AllEditTriggers31Editing starts for all above actions.

enum QAbstractItemView::ScrollHint

ConstantValueDescription
QAbstractItemView::EnsureVisible0Scroll to ensure that the item is visible.
QAbstractItemView::PositionAtTop1Scroll to position the item at the top of the viewport.
QAbstractItemView::PositionAtBottom2Scroll to position the item at the bottom of the viewport.
QAbstractItemView::PositionAtCenter3Scroll to position the item at the center of the viewport.

enum QAbstractItemView::ScrollMode

Describes how the scrollbar should behave. When setting the scroll mode to ScrollPerPixel the single step size will adjust automatically unless it was set explicitly using setSingleStep(). The automatic adjustment can be restored by setting the single step size to -1.

ConstantValueDescription
QAbstractItemView::ScrollPerItem0The view will scroll the contents one item at a time.
QAbstractItemView::ScrollPerPixel1The view will scroll the contents one pixel at a time.

This enum was introduced or modified in Qt 4.2.

enum QAbstractItemView::SelectionBehavior

ConstantValueDescription
QAbstractItemView::SelectItems0Selecting single items.
QAbstractItemView::SelectRows1Selecting only rows.
QAbstractItemView::SelectColumns2Selecting only columns.

enum QAbstractItemView::SelectionMode

This enum indicates how the view responds to user selections:

ConstantValueDescription
QAbstractItemView::SingleSelection1When the user selects an item, any already-selected item becomes unselected. It is possible for the user to deselect the selected item by pressing the Ctrl key when clicking the selected item.
QAbstractItemView::ContiguousSelection4When the user selects an item in the usual way, the selection is cleared and the new item selected. However, if the user presses the Shift key while clicking on an item, all items between the current item and the clicked item are selected or unselected, depending on the state of the clicked item.
QAbstractItemView::ExtendedSelection3When the user selects an item in the usual way, the selection is cleared and the new item selected. However, if the user presses the Ctrl key when clicking on an item, the clicked item gets toggled and all other items are left untouched. If the user presses the Shift key while clicking on an item, all items between the current item and the clicked item are selected or unselected, depending on the state of the clicked item. Multiple items can be selected by dragging the mouse over them.
QAbstractItemView::MultiSelection2When the user selects an item in the usual way, the selection status of that item is toggled and the other items are left alone. Multiple items can be toggled by dragging the mouse over them.
QAbstractItemView::NoSelection0Items cannot be selected.

The most commonly used modes are SingleSelection and ExtendedSelection.

enum QAbstractItemView::State

Describes the different states the view can be in. This is usually only interesting when reimplementing your own view.

ConstantValueDescription
QAbstractItemView::NoState0The is the default state.
QAbstractItemView::DraggingState1The user is dragging items.
QAbstractItemView::DragSelectingState2The user is selecting items.
QAbstractItemView::EditingState3The user is editing an item in a widget editor.
QAbstractItemView::ExpandingState4The user is opening a branch of items.
QAbstractItemView::CollapsingState5The user is closing a branch of items.
QAbstractItemView::AnimatingState6The item view is performing an animation.

Property Documentation

alternatingRowColors : bool

This property holds whether to draw the background using alternating colors

If this property is true, the item background will be drawn using QPalette::Base and QPalette::AlternateBase; otherwise the background will be drawn using the QPalette::Base color.

By default, this property is false.

Access functions:

bool alternatingRowColors() const
void setAlternatingRowColors(bool enable)

autoScroll : bool

This property holds whether autoscrolling in drag move events is enabled

If this property is set to true (the default), the QAbstractItemView automatically scrolls the contents of the view if the user drags within 16 pixels of the viewport edge. If the current item changes, then the view will scroll automatically to ensure that the current item is fully visible.

This property only works if the viewport accepts drops. Autoscroll is switched off by setting this property to false.

Access functions:

bool hasAutoScroll() const
void setAutoScroll(bool enable)

autoScrollMargin : int

This property holds the size of the area when auto scrolling is triggered

This property controls the size of the area at the edge of the viewport that triggers autoscrolling. The default value is 16 pixels.

This property was introduced in Qt 4.4.

Access functions:

int autoScrollMargin() const
void setAutoScrollMargin(int margin)

defaultDropAction : Qt::DropAction

This property holds the drop action that will be used by default in QAbstractItemView::drag()

If the property is not set, the drop action is CopyAction when the supported actions support CopyAction.

This property was introduced in Qt 4.6.

Access functions:

Qt::DropAction defaultDropAction() const
void setDefaultDropAction(Qt::DropAction dropAction)

See also showDropIndicator and dragDropOverwriteMode.

dragDropMode : DragDropMode

This property holds the drag and drop event the view will act upon

This property was introduced in Qt 4.2.

Access functions:

QAbstractItemView::DragDropMode dragDropMode() const
void setDragDropMode(QAbstractItemView::DragDropMode behavior)

See also showDropIndicator and dragDropOverwriteMode.

dragDropOverwriteMode : bool

This property holds the view's drag and drop behavior

If its value is true, the selected data will overwrite the existing item data when dropped, while moving the data will clear the item. If its value is false, the selected data will be inserted as a new item when the data is dropped. When the data is moved, the item is removed as well.

The default value is false, as in the QListView and QTreeView subclasses. In the QTableView subclass, on the other hand, the property has been set to true.

Note: This is not intended to prevent overwriting of items. The model's implementation of flags() should do that by not returning Qt::ItemIsDropEnabled.

This property was introduced in Qt 4.2.

Access functions:

bool dragDropOverwriteMode() const
void setDragDropOverwriteMode(bool overwrite)

See also dragDropMode.

dragEnabled : bool

This property holds whether the view supports dragging of its own items

Access functions:

bool dragEnabled() const
void setDragEnabled(bool enable)

See also showDropIndicator, DragDropMode, dragDropOverwriteMode, and acceptDrops.

editTriggers : EditTriggers

This property holds which actions will initiate item editing

This property is a selection of flags defined by EditTrigger, combined using the OR operator. The view will only initiate the editing of an item if the action performed is set in this property.

Access functions:

QAbstractItemView::EditTriggers editTriggers() const
void setEditTriggers(QAbstractItemView::EditTriggers triggers)

horizontalScrollMode : ScrollMode

how the view scrolls its contents in the horizontal direction

This property controls how the view scroll its contents horizontally. Scrolling can be done either per pixel or per item. Its default value comes from the style via the QStyle::SH_ItemView_ScrollMode style hint.

This property was introduced in Qt 4.2.

Access functions:

QAbstractItemView::ScrollMode horizontalScrollMode() const
void setHorizontalScrollMode(QAbstractItemView::ScrollMode mode)
void resetHorizontalScrollMode()

iconSize : QSize

This property holds the size of items' icons

Setting this property when the view is visible will cause the items to be laid out again.

Access functions:

QSize iconSize() const
void setIconSize(const QSize &size)

Notifier signal:

void iconSizeChanged(const QSize &size)

selectionBehavior : SelectionBehavior

This property holds which selection behavior the view uses

This property holds whether selections are done in terms of single items, rows or columns.

Access functions:

QAbstractItemView::SelectionBehavior selectionBehavior() const
void setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)

See also SelectionMode and SelectionBehavior.

selectionMode : SelectionMode

This property holds which selection mode the view operates in

This property controls whether the user can select one or many items and, in many-item selections, whether the selection must be a continuous range of items.

Access functions:

QAbstractItemView::SelectionMode selectionMode() const
void setSelectionMode(QAbstractItemView::SelectionMode mode)

See also SelectionMode and SelectionBehavior.

showDropIndicator : bool

This property holds whether the drop indicator is shown when dragging items and dropping.

Access functions:

bool showDropIndicator() const
void setDropIndicatorShown(bool enable)

See also dragEnabled, DragDropMode, dragDropOverwriteMode, and acceptDrops.

tabKeyNavigation : bool

This property holds whether item navigation with tab and backtab is enabled.

Access functions:

bool tabKeyNavigation() const
void setTabKeyNavigation(bool enable)

textElideMode : Qt::TextElideMode

This property holds the position of the "..." in elided text.

The default value for all item views is Qt::ElideRight.

Access functions:

Qt::TextElideMode textElideMode() const
void setTextElideMode(Qt::TextElideMode mode)

verticalScrollMode : ScrollMode

how the view scrolls its contents in the vertical direction

This property controls how the view scroll its contents vertically. Scrolling can be done either per pixel or per item. Its default value comes from the style via the QStyle::SH_ItemView_ScrollMode style hint.

This property was introduced in Qt 4.2.

Access functions:

QAbstractItemView::ScrollMode verticalScrollMode() const
void setVerticalScrollMode(QAbstractItemView::ScrollMode mode)
void resetVerticalScrollMode()