QAbstractItemView 类

QAbstractItemView 类为项视图类提供基本功能。 更多...

Header: #include <QAbstractItemView>
CMake: find_package(Qt6 COMPONENTS Widgets REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QAbstractScrollArea
继承者:

QColumnView , QHeaderView , QListView , QTableView ,和 QTreeView

公共类型

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

特性

公共函数

QAbstractItemView (QWidget * parent = nullptr)
virtual ~QAbstractItemView ()
bool alternatingRowColors () const
int autoScrollMargin () const
void closePersistentEditor (const QModelIndex & index )
QModelIndex currentIndex () 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
virtual QModelIndex indexAt (const QPoint & point ) const = 0
QWidget * indexWidget (const QModelIndex & index ) const
bool isPersistentEditorOpen (const QModelIndex & index ) const
QAbstractItemDelegate * itemDelegate () const
QAbstractItemDelegate * itemDelegateForColumn (int column ) const
virtual QAbstractItemDelegate * itemDelegateForIndex (const QModelIndex & index ) const
QAbstractItemDelegate * itemDelegateForRow (int row ) const
virtual void keyboardSearch (const QString & search )
QAbstractItemModel * model () const
void openPersistentEditor (const QModelIndex & index )
void resetHorizontalScrollMode ()
void resetVerticalScrollMode ()
QModelIndex rootIndex () const
virtual void scrollTo (const QModelIndex & index , QAbstractItemView::ScrollHint hint = EnsureVisible) = 0
QAbstractItemView::SelectionBehavior selectionBehavior () const
QAbstractItemView::SelectionMode selectionMode () const
QItemSelectionModel * selectionModel () 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 setIndexWidget (const QModelIndex & index , QWidget * widget )
void setItemDelegate (QAbstractItemDelegate * delegate )
void setItemDelegateForColumn (int column , QAbstractItemDelegate * delegate )
void setItemDelegateForRow (int row , QAbstractItemDelegate * delegate )
virtual void setModel (QAbstractItemModel * model )
void setSelectionBehavior (QAbstractItemView::SelectionBehavior behavior )
void setSelectionMode (QAbstractItemView::SelectionMode mode )
virtual void setSelectionModel (QItemSelectionModel * selectionModel )
void setTabKeyNavigation (bool enable )
void setTextElideMode (Qt::TextElideMode mode )
void setVerticalScrollMode (QAbstractItemView::ScrollMode mode )
bool showDropIndicator () const
virtual int sizeHintForColumn (int column ) const
QSize sizeHintForIndex (const QModelIndex & index ) const
virtual int sizeHintForRow (int row ) const
bool tabKeyNavigation () const
Qt::TextElideMode textElideMode () const
QAbstractItemView::ScrollMode verticalScrollMode () const
virtual QRect visualRect (const QModelIndex & index ) const = 0

重实现公共函数

virtual QVariant inputMethodQuery (Qt::InputMethodQuery query ) const override

公共槽

void clearSelection ()
void edit (const QModelIndex & index )
virtual void reset ()
void scrollToBottom ()
void scrollToTop ()
virtual void selectAll ()
void setCurrentIndex (const QModelIndex & index )
virtual void setRootIndex (const QModelIndex & index )
void update (const QModelIndex & index )

信号

void activated (const QModelIndex & index )
void clicked (const QModelIndex & index )
void doubleClicked (const QModelIndex & index )
void entered (const QModelIndex & index )
void iconSizeChanged (const QSize & size )
void pressed (const QModelIndex & index )
void viewportEntered ()

保护类型

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

保护函数

QPoint dirtyRegionOffset () const
QAbstractItemView::DropIndicatorPosition dropIndicatorPosition () const
virtual bool edit (const QModelIndex & index , QAbstractItemView::EditTrigger trigger , QEvent * event )
void executeDelayedItemsLayout ()
virtual int horizontalOffset () const = 0
virtual void initViewItemOption (QStyleOptionViewItem * option ) const
virtual bool isIndexHidden (const QModelIndex & index ) const = 0
virtual QModelIndex moveCursor (QAbstractItemView::CursorAction cursorAction , Qt::KeyboardModifiers modifiers ) = 0
void scheduleDelayedItemsLayout ()
void scrollDirtyRegion (int dx , int dy )
virtual QModelIndexList selectedIndexes () const
virtual QItemSelectionModel::SelectionFlags selectionCommand (const QModelIndex & index , const QEvent * event = nullptr) const
void setDirtyRegion (const QRegion & region )
virtual void setSelection (const QRect & rect , QItemSelectionModel::SelectionFlags flags ) = 0
void setState (QAbstractItemView::State state )
virtual void startDrag (Qt::DropActions supportedActions )
QAbstractItemView::State state () const
virtual int verticalOffset () const = 0
virtual QRegion visualRegionForSelection (const QItemSelection & selection ) const = 0

重实现保护函数

virtual void dragEnterEvent (QDragEnterEvent * event ) override
virtual void dragLeaveEvent (QDragLeaveEvent * event ) override
virtual void dragMoveEvent (QDragMoveEvent * event ) override
virtual void dropEvent (QDropEvent * event ) override
virtual bool event (QEvent * event ) override
virtual bool eventFilter (QObject * object , QEvent * event ) override
virtual void focusInEvent (QFocusEvent * event ) override
virtual bool focusNextPrevChild (bool next ) override
virtual void focusOutEvent (QFocusEvent * event ) override
virtual void inputMethodEvent (QInputMethodEvent * event ) override
virtual void keyPressEvent (QKeyEvent * event ) override
virtual void mouseDoubleClickEvent (QMouseEvent * event ) override
virtual void mouseMoveEvent (QMouseEvent * event ) override
virtual void mousePressEvent (QMouseEvent * event ) override
virtual void mouseReleaseEvent (QMouseEvent * event ) override
virtual void resizeEvent (QResizeEvent * event ) override
virtual void timerEvent (QTimerEvent * event ) override
virtual bool viewportEvent (QEvent * event ) override
virtual QSize viewportSizeHint () const override

保护槽

virtual void closeEditor (QWidget * editor , QAbstractItemDelegate::EndEditHint hint )
virtual void commitData (QWidget * editor )
virtual void currentChanged (const QModelIndex & current , const QModelIndex & previous )
virtual void dataChanged (const QModelIndex & topLeft , const QModelIndex & bottomRight , const QList<int> & roles = QList<int>())
virtual void editorDestroyed (QObject * editor )
virtual void rowsAboutToBeRemoved (const QModelIndex & parent , int start , int end )
virtual void rowsInserted (const QModelIndex & parent , int start , int end )
virtual void selectionChanged (const QItemSelection & selected , const QItemSelection & deselected )
virtual void updateGeometries ()

详细描述

QAbstractItemView 类是基类用于每个标准视图使用 QAbstractItemModel 。QAbstractItemView 是本身不可以被实例化的抽象类。它提供透过信号和槽机制与模型进行互操作的标准接口,使子类能够随着其模型的改变保持最新。此类为键盘和鼠标导航、视口卷动、项编辑及选定提供标准支持。键盘导航实现了此功能:

功能
方向键 改变当前项并选择它。
Ctrl + 方向键 改变当前项但不选择它。
Shift + 方向键 更改当前项并选择它。先前选中项不会被取消选择。
Ctr+Space 触发当前项的选定。
Tab/Backtab 将当前项更改为下一/上一项。
Home/End 选择模型中的第一/最后项。
Page up/Page down 按视图中的可见行数向上/向下卷动展示行。
Ctrl+A 选择模型中的所有项。

注意,上表假定 选定模式 允许操作。例如,无法选择项,若选定模式为 QAbstractItemView::NoSelection .

QAbstractItemView 类是一种 模型/视图类 且属于 Qt 的 模型/视图框架 .

继承 QAbstractItemView 的视图类只需要实现自己的特定视图功能,譬如绘制项、返回项几何体、查找项、等。

QAbstractItemView 提供常见槽,譬如 edit () 和 setCurrentIndex ()。还提供很多保护槽,包括 dataChanged (), rowsInserted (), rowsAboutToBeRemoved (), selectionChanged (),和 currentChanged ().

根项的返回通过 rootIndex (),和当前项通过 currentIndex ()。要确保项可见使用 scrollTo ().

某些 QAbstractItemView 函数涉及卷动,例如 setHorizontalScrollMode () 和 setVerticalScrollMode ()。要设置滚动条的范围,例如,可以重实现视图的 resizeEvent () 函数:

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

注意,范围不更新,直到小部件被展示为止。

几个其它函数涉及选定控制;例如 setSelectionMode (),和 setSelectionBehavior ()。此类为处理提供默认选定模型 ( selectionModel ()),但可以替换这通过使用 setSelectionModel () 采用实例 QItemSelectionModel .

要完全控制项的显示和编辑,可以指定委托采用 setItemDelegate ().

QAbstractItemView 提供了许多保护函数。某些涉及编辑,例如, edit (),和 commitData (),而其它的是键盘和鼠标事件处理程序。

注意: 若继承 QAbstractItemView 并打算更新视口内容,应使用 viewport-> update () 而不是 update() 因为所有描绘操作都发生在视口中。

另请参阅 视图类 , 模型/视图编程 , QAbstractItemModel ,和 图表范例 .

成员类型文档编制

enum QAbstractItemView:: CursorAction

此枚举描述在项之间导航的不同方式,

常量 描述
QAbstractItemView::MoveUp 0 移至当前项的上方项。
QAbstractItemView::MoveDown 1 移至当前项的下方项。
QAbstractItemView::MoveLeft 2 移动到当前项的左侧项。
QAbstractItemView::MoveRight 3 移动到当前项的右侧项。
QAbstractItemView::MoveHome 4 移至左上角项。
QAbstractItemView::MoveEnd 5 移动到右下角项。
QAbstractItemView::MovePageUp 6 移至当前项上一页。
QAbstractItemView::MovePageDown 7 移至当前项下一页。
QAbstractItemView::MoveNext 8 移至当前项之后项。
QAbstractItemView::MovePrevious 9 移至当前项之前项。

另请参阅 moveCursor ().

enum QAbstractItemView:: DragDropMode

描述视图可以行动的各种拖放事件。默认情况下,视图不支持拖曳或掉落 ( NoDragDrop ).

常量 描述
QAbstractItemView::NoDragDrop 0 不支持拖曳或掉落。
QAbstractItemView::DragOnly 1 视图支持拖曳自己的项
QAbstractItemView::DropOnly 2 视图接受掉落
QAbstractItemView::DragDrop 3 视图支持拖拽和掉落两者
QAbstractItemView::InternalMove 4 视图接受移动 ( 非拷贝 ) 操作仅来自自身。

注意,使用模型需要提供对拖放操作的支持。

另请参阅 setDragDropMode () 和 将拖放用于项视图 .

enum QAbstractItemView:: DropIndicatorPosition

此枚举指示掉落指示器相对当前鼠标位置的索引位置:

常量 描述
QAbstractItemView::OnItem 0 项将掉落在索引处。
QAbstractItemView::AboveItem 1 项将掉落在索引上方。
QAbstractItemView::BelowItem 2 项将掉落在索引下方。
QAbstractItemView::OnViewport 3 项将掉落在没有项的视口区域。各视图处理视口掉落项的方式,从属正使用的底层模型行为。

enum QAbstractItemView:: EditTrigger
flags QAbstractItemView:: EditTriggers

此枚举描述将初始项编辑的动作。

常量 描述
QAbstractItemView::NoEditTriggers 0 没有编辑可能。
QAbstractItemView::CurrentChanged 1 开始编辑每当当前项改变时。
QAbstractItemView::DoubleClicked 2 开始编辑当双击项时。
QAbstractItemView::SelectedClicked 4 开始编辑当点击已选中项时。
QAbstractItemView::EditKeyPressed 8 开始编辑当在项上按下平台编辑键时。
QAbstractItemView::AnyKeyPressed 16 开始编辑当在项上按下任意键时。
QAbstractItemView::AllEditTriggers 31 开始编辑对于上述所有动作。

EditTriggers 类型是 typedef 对于 QFlags <EditTrigger>。它存储 EditTrigger 值的 OR 组合。

enum QAbstractItemView:: ScrollHint

常量 描述
QAbstractItemView::EnsureVisible 0 卷动以确保项可见。
QAbstractItemView::PositionAtTop 1 卷动以将项放置在视口顶部。
QAbstractItemView::PositionAtBottom 2 卷动以定位到视口底部项。
QAbstractItemView::PositionAtCenter 3 卷动以定位到视口中心项。

enum QAbstractItemView:: ScrollMode

描述滚动条的行为应如何。当把卷动模式设为 ScrollPerPixel 时,将自动调节单步大小,除非它被明确设置使用 setSingleStep() 。可以通过将单步大小设为-1 以还原自动调节。

常量 描述
QAbstractItemView::ScrollPerItem 0 视图每次卷动内容一项。
QAbstractItemView::ScrollPerPixel 1 视图每次卷动内容一像素。

enum QAbstractItemView:: SelectionBehavior

常量 描述
QAbstractItemView::SelectItems 0 选择单个项。
QAbstractItemView::SelectRows 1 仅选择行。
QAbstractItemView::SelectColumns 2 仅选择列。

enum QAbstractItemView:: SelectionMode

此枚举指示视图如何响应用户选择:

常量 描述
QAbstractItemView::SingleSelection 1 当用户选择项时,任何已选中项变为未选中。用户通过按下 Ctrl 键取消对选中项的选择是可能的,当点击选中项时。
QAbstractItemView::ContiguousSelection 4 When 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::ExtendedSelection 3 When 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::MultiSelection 2 When 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::NoSelection 0 无法选择项。

最常用模式为 SingleSelection 和 ExtendedSelection。

enum QAbstractItemView:: State

描述视图可以处于的不同状态。通常,这只对重实现自己的视图感兴趣。

常量 描述
QAbstractItemView::NoState 0 这是默认状态。
QAbstractItemView::DraggingState 1 用户正在拖曳项。
QAbstractItemView::DragSelectingState 2 用户正在选择项。
QAbstractItemView::EditingState 3 用户正在 Widget 编辑器中编辑项。
QAbstractItemView::ExpandingState 4 用户正在打开项分支。
QAbstractItemView::CollapsingState 5 用户正在关闭项分支。
QAbstractItemView::AnimatingState 6 项视图正在履行动画。

特性文档编制

alternatingRowColors : bool

此特性保持是否使用替代颜色绘制背景

若此特性为 true ,将绘制项背景使用 QPalette::Base and QPalette::AlternateBase ;否则将绘制项背景使用 QPalette::Base 颜色。

默认情况下此特性为 false .

访问函数:

bool alternatingRowColors () const
void setAlternatingRowColors (bool enable )

autoScroll : bool

此特性保持是否在拖拽移动事件中启用自动卷动

若此特性被设为 true (默认), 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.

访问函数:

bool hasAutoScroll () const
void setAutoScroll (bool enable )

autoScrollMargin : int

此特性保持区域的大小当触发自动卷动时

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

访问函数:

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.

访问函数:

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

另请参阅 showDropIndicator and dragDropOverwriteMode .

dragDropMode : DragDropMode

此特性保持视图将进行拖放事件行动

访问函数:

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

另请参阅 showDropIndicator and dragDropOverwriteMode .

dragDropOverwriteMode : bool

此特性保持视图的拖放行为

若其值为 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.

默认值为 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 .

访问函数:

bool dragDropOverwriteMode () const
void setDragDropOverwriteMode (bool overwrite )

另请参阅 dragDropMode .

dragEnabled : bool

此特性保持视图是否支持拖拽其自己的项

访问函数:

bool dragEnabled () const
void setDragEnabled (bool enable )

另请参阅 showDropIndicator , DragDropMode , dragDropOverwriteMode ,和 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.

访问函数:

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

horizontalScrollMode : ScrollMode

视图如何在水平方向卷动其内容

此特性控制视图如何水平卷动其内容。卷动可以按像素或按项进行。它的默认值来自样式凭借 QStyle::SH_ItemView_ScrollMode 样式提示。

访问函数:

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

iconSize : QSize

此特性保持项图标的大小

设置此特性将导致项被再次布置,当视图可见时。

访问函数:

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

通知程序信号:

void iconSizeChanged (const QSize & size )

selectionBehavior : SelectionBehavior

此特性保持视图使用的选定行为

此特性保持是否按单项、单行或单列进行选择。

访问函数:

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

另请参阅 SelectionMode and SelectionBehavior .

selectionMode : SelectionMode

此特性保持在哪种选择模式下操作视图

此特性控制用户是否可以选择一个或多个项,和在多项选定中,选定是否必须为连续范围项。

访问函数:

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

另请参阅 SelectionMode and SelectionBehavior .

showDropIndicator : bool

此特性保持是否展示掉落指示器,当拖放项时。

访问函数:

bool showDropIndicator () const
void setDropIndicatorShown (bool enable )

另请参阅 dragEnabled , DragDropMode , dragDropOverwriteMode ,和 acceptDrops .

tabKeyNavigation : bool

此特性保持采用 tab 和 backtab 的项导航是否被启用。

访问函数:

bool tabKeyNavigation () const
void setTabKeyNavigation (bool enable )

textElideMode : Qt::TextElideMode

此特性保持 ... 在省略文本中的位置。

所有项视图的默认值为 Qt::ElideRight .

访问函数:

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

verticalScrollMode : ScrollMode

视图如何在垂直方向卷动其内容

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 样式提示。

访问函数:

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

成员函数文档编制

QAbstractItemView:: QAbstractItemView ( QWidget * parent = nullptr)

构造抽象项视图采用给定 parent .

[signal] void QAbstractItemView:: activated (const QModelIndex & index )

此信号被发射当项指定通过 index is activated by the user. How to activate items depends on the platform; e.g., by single- or double-clicking the item, or by pressing the Return or Enter key when the item is current.

另请参阅 clicked (), doubleClicked (), entered (),和 pressed ().

[slot] void QAbstractItemView:: clearSelection ()

Deselects all selected items. The current index will not be changed.

另请参阅 setSelection () 和 selectAll ().

[signal] void QAbstractItemView:: clicked (const QModelIndex & index )

This signal is emitted when a mouse button is left-clicked. The item the mouse was clicked on is specified by index . The signal is only emitted when the index is valid.

另请参阅 activated (), doubleClicked (), entered (),和 pressed ().

[virtual protected slot] void QAbstractItemView:: closeEditor ( QWidget * editor , QAbstractItemDelegate::EndEditHint hint )

关闭给定 editor , and releases it. The hint is used to specify how the view should respond to the end of the editing operation. For example, the hint may indicate that the next item in the view should be opened for editing.

另请参阅 edit () 和 commitData ().

[virtual protected slot] void QAbstractItemView:: commitData ( QWidget * editor )

提交数据在 editor 到模型。

另请参阅 closeEditor ().

[virtual protected slot] void QAbstractItemView:: currentChanged (const QModelIndex & current , const QModelIndex & previous )

This slot is called when a new item becomes the current item. The previous current item is specified by the previous index, and the new item by the current index.

If you want to know about changes to items see the dataChanged () 信号。

[virtual protected slot] void QAbstractItemView:: dataChanged (const QModelIndex & topLeft , const QModelIndex & bottomRight , const QList < int > & roles = QList<int>())

This slot is called when items with the given roles are changed in the model. The changed items are those from topLeft to bottomRight inclusive. If just one item is changed topLeft == bottomRight .

roles which have been changed can either be an empty container (meaning everything has changed), or a non-empty container with the subset of roles which have changed.

注意: : Qt::ToolTipRole is not honored by dataChanged() in the views provided by Qt.

[signal] void QAbstractItemView:: doubleClicked (const QModelIndex & index )

This signal is emitted when a mouse button is double-clicked. The item the mouse was double-clicked on is specified by index . The signal is only emitted when the index is valid.

另请参阅 clicked () 和 activated ().

[slot] void QAbstractItemView:: edit (const QModelIndex & index )

Starts editing the item corresponding to the given index 若它可编辑。

Note that this function does not change the current index. Since the current index defines the next and previous items to edit, users may find that keyboard navigation does not work as expected. To provide consistent navigation behavior, call setCurrentIndex () 在此函数采用相同模型索引之前。

另请参阅 QModelIndex::flags ().

[virtual protected slot] void QAbstractItemView:: editorDestroyed ( QObject * editor )

This function is called when the given editor has been destroyed.

另请参阅 closeEditor ().

[signal] void QAbstractItemView:: entered (const QModelIndex & index )

This signal is emitted when the mouse cursor enters the item specified by index . Mouse tracking needs to be enabled for this feature to work.

另请参阅 viewportEntered (), activated (), clicked (), doubleClicked (),和 pressed ().

[signal] void QAbstractItemView:: pressed (const QModelIndex & index )

This signal is emitted when a mouse button is pressed. The item the mouse was pressed on is specified by index . The signal is only emitted when the index is valid.

使用 QGuiApplication::mouseButtons () function to get the state of the mouse buttons.

另请参阅 activated (), clicked (), doubleClicked (),和 entered ().

[virtual slot] void QAbstractItemView:: reset ()

重置视图的内部状态。

警告: This function will reset open editors, scroll bar positions, selections, etc. Existing changes will not be committed. If you would like to save your changes when resetting the view, you can reimplement this function, commit your changes, and then call the superclass' implementation.

[virtual protected slot] void QAbstractItemView:: rowsAboutToBeRemoved (const QModelIndex & parent , int start , int end )

This slot is called when rows are about to be removed. The deleted rows are those under the given parent from start to end 包含在内。

另请参阅 rowsInserted ().

[virtual protected slot] void QAbstractItemView:: rowsInserted (const QModelIndex & parent , int start , int end )

This slot is called when rows are inserted. The new rows are those under the given parent from start to end inclusive. The base class implementation calls fetchMore() on the model to check for more data.

另请参阅 rowsAboutToBeRemoved ().

[slot] void QAbstractItemView:: scrollToBottom ()

卷动视图到底部。

另请参阅 scrollTo () 和 scrollToTop ().

[slot] void QAbstractItemView:: scrollToTop ()

卷动视图到顶部。

另请参阅 scrollTo () 和 scrollToBottom ().

[virtual slot] void QAbstractItemView:: selectAll ()

Selects all items in the view. This function will use the selection behavior set on the view when selecting.

另请参阅 setSelection (), selectedIndexes (),和 clearSelection ().

[virtual protected slot] void QAbstractItemView:: selectionChanged (const QItemSelection & selected , const QItemSelection & deselected )

This slot is called when the selection is changed. The previous selection (which may be empty), is specified by deselected , and the new selection by selected .

另请参阅 setSelection ().

[slot] void QAbstractItemView:: setCurrentIndex (const QModelIndex & index )

将当前项设为是项在 index .

除非当前选定方式为 NoSelection ,项也会被选中。注意:此函数还会更新用户履行任何新选定的起始位置。

要把项设为当前项而不选择它,调用

selectionModel()->setCurrentIndex(index, QItemSelectionModel::NoUpdate);

另请参阅 currentIndex (), currentChanged (),和 selectionMode .

[virtual slot] void QAbstractItemView:: setRootIndex (const QModelIndex & index )

Sets the root item to the item at the given index .

另请参阅 rootIndex ().

[slot] void QAbstractItemView:: update (const QModelIndex & index )

Updates the area occupied by the given index .

[virtual protected slot] void QAbstractItemView:: updateGeometries ()

Updates the geometry of the child widgets of the view.

[signal] void QAbstractItemView:: viewportEntered ()

This signal is emitted when the mouse cursor enters the viewport. Mouse tracking needs to be enabled for this feature to work.

另请参阅 entered ().

[virtual] QAbstractItemView:: ~QAbstractItemView ()

销毁视图。

void QAbstractItemView:: closePersistentEditor (const QModelIndex & index )

Closes the persistent editor for the item at the given index .

另请参阅 openPersistentEditor () 和 isPersistentEditorOpen ().

QModelIndex QAbstractItemView:: currentIndex () const

返回当前项的模型索引。

另请参阅 setCurrentIndex ().

[protected] QPoint QAbstractItemView:: dirtyRegionOffset () const

返回视图脏区域的偏移。

若使用 scrollDirtyRegion () 和实现 paintEvent () in a subclass of QAbstractItemView , you should translate the area given by the paint event with the offset returned from this function.

另请参阅 scrollDirtyRegion () 和 setDirtyRegion ().

[override virtual protected] void QAbstractItemView:: dragEnterEvent ( QDragEnterEvent * event )

重实现: QAbstractScrollArea::dragEnterEvent (QDragEnterEvent *event).

此函数被调用采用给定 event when a drag and drop operation enters the widget. If the drag is over a valid dropping place (e.g. over an item that accepts drops), the event is accepted; otherwise it is ignored.

另请参阅 dropEvent () 和 startDrag ().

[override virtual protected] void QAbstractItemView:: dragLeaveEvent ( QDragLeaveEvent * event )

重实现: QAbstractScrollArea::dragLeaveEvent (QDragLeaveEvent *event).

This function is called when the item being dragged leaves the view. The event describes the state of the drag and drop operation.

[override virtual protected] void QAbstractItemView:: dragMoveEvent ( QDragMoveEvent * event )

重实现: QAbstractScrollArea::dragMoveEvent (QDragMoveEvent *event).

This function is called continuously with the given event during a drag and drop operation over the widget. It can cause the view to scroll if, for example, the user drags a selection to view's right or bottom edge. In this case, the event will be accepted; otherwise it will be ignored.

另请参阅 dropEvent () 和 startDrag ().

[override virtual protected] void QAbstractItemView:: dropEvent ( QDropEvent * event )

重实现: QAbstractScrollArea::dropEvent (QDropEvent *event).

此函数被调用采用给定 event when a drop event occurs over the widget. If the model accepts the even position the drop event is accepted; otherwise it is ignored.

另请参阅 startDrag ().

[protected] QAbstractItemView::DropIndicatorPosition QAbstractItemView:: dropIndicatorPosition () const

Returns the position of the drop indicator in relation to the closest item.

[virtual protected] bool QAbstractItemView:: edit (const QModelIndex & index , QAbstractItemView::EditTrigger trigger , QEvent * event )

Starts editing the item at index , creating an editor if necessary, and returns true if the view's State 现为 EditingState ;否则返回 false .

The action that caused the editing process is described by trigger , and the associated event is specified by event .

Editing can be forced by specifying the trigger to be QAbstractItemView::AllEditTriggers .

另请参阅 closeEditor ().

[override virtual protected] bool QAbstractItemView:: event ( QEvent * event )

重实现: QAbstractScrollArea::event (QEvent *event).

[override virtual protected] bool QAbstractItemView:: eventFilter ( QObject * object , QEvent * event )

重实现: QObject::eventFilter (QObject *watched, QEvent *event).

[protected] void QAbstractItemView:: executeDelayedItemsLayout ()

Executes the scheduled layouts without waiting for the event processing to begin.

另请参阅 scheduleDelayedItemsLayout ().

[override virtual protected] void QAbstractItemView:: focusInEvent ( QFocusEvent * event )

重实现: QWidget::focusInEvent (QFocusEvent *event).

此函数被调用采用给定 event when the widget obtains the focus. By default, the event is ignored.

另请参阅 setFocus () 和 focusOutEvent ().

[override virtual protected] bool QAbstractItemView:: focusNextPrevChild ( bool next )

重实现: QWidget::focusNextPrevChild (bool next).

[override virtual protected] void QAbstractItemView:: focusOutEvent ( QFocusEvent * event )

重实现: QWidget::focusOutEvent (QFocusEvent *event).

此函数被调用采用给定 event when the widget loses the focus. By default, the event is ignored.

另请参阅 clearFocus () 和 focusInEvent ().

[pure virtual protected] int QAbstractItemView:: horizontalOffset () const

返回视图的水平偏移。

在基类中,这是纯虚函数。

另请参阅 verticalOffset ().

[pure virtual] QModelIndex QAbstractItemView:: indexAt (const QPoint & point ) const

Returns the model index of the item at the viewport coordinates point .

在基类中,这是纯虚函数。

另请参阅 visualRect ().

QWidget *QAbstractItemView:: indexWidget (const QModelIndex & index ) const

返回用于项的 Widget 在给定 index .

另请参阅 setIndexWidget ().

[virtual protected, since 6.0] void QAbstractItemView:: initViewItemOption ( QStyleOptionViewItem * option ) const

初始化 option structure with the view's palette, font, state, alignments etc.

注意: Implementations of this methods should check the version of the structure received, populate all members the implementation is familiar with, and set the version member to the one supported by the implementation before returning.

该函数在 Qt 6.0 引入。

[override virtual protected] void QAbstractItemView:: inputMethodEvent ( QInputMethodEvent * event )

重实现: QWidget::inputMethodEvent (QInputMethodEvent *event).

[override virtual] QVariant QAbstractItemView:: inputMethodQuery ( Qt::InputMethodQuery query ) const

重实现: QWidget::inputMethodQuery (Qt::InputMethodQuery query) const.

[pure virtual protected] bool QAbstractItemView:: isIndexHidden (const QModelIndex & index ) const

返回 true 若项引用通过给定 index is hidden in the view, otherwise returns false .

Hiding is a view specific feature. For example in TableView a column can be marked as hidden or a row in the TreeView.

在基类中,这是纯虚函数。

[since 5.10] bool QAbstractItemView:: isPersistentEditorOpen (const QModelIndex & index ) const

返回项是否打开持久编辑器在索引 index .

该函数在 Qt 5.10 引入。

另请参阅 openPersistentEditor () 和 closePersistentEditor ().

QAbstractItemDelegate *QAbstractItemView:: itemDelegate () const

Returns the item delegate used by this view and model. This is either one set with setItemDelegate (), or the default one.

另请参阅 setItemDelegate ().

QAbstractItemDelegate *QAbstractItemView:: itemDelegateForColumn ( int column ) const

Returns the item delegate used by this view and model for the given column . You can call itemDelegate () to get a pointer to the current delegate for a given index.

另请参阅 setItemDelegateForColumn (), itemDelegateForRow (),和 itemDelegate ().

[virtual, since 6.0] QAbstractItemDelegate *QAbstractItemView:: itemDelegateForIndex (const QModelIndex & index ) const

Returns the item delegate used by this view and model for the given index .

该函数在 Qt 6.0 引入。

另请参阅 setItemDelegate (), setItemDelegateForRow (),和 setItemDelegateForColumn ().

QAbstractItemDelegate *QAbstractItemView:: itemDelegateForRow ( int row ) const

Returns the item delegate used by this view and model for the given row ,或 nullptr if no delegate has been assigned. You can call itemDelegate () to get a pointer to the current delegate for a given index.

另请参阅 setItemDelegateForRow (), itemDelegateForColumn (),和 setItemDelegate ().

[override virtual protected] void QAbstractItemView:: keyPressEvent ( QKeyEvent * event )

重实现: QAbstractScrollArea::keyPressEvent (QKeyEvent *e).

此函数被调用采用给定 event when a key event is sent to the widget. The default implementation handles basic cursor movement, e.g. Up, Down, Left, Right, Home, PageUp, and PageDown; the activated () signal is emitted if the current index is valid and the activation key is pressed (e.g. Enter or Return, depending on the platform). This function is where editing is initiated by key press, e.g. if F2 is pressed.

另请参阅 edit (), moveCursor (), keyboardSearch (),和 tabKeyNavigation .

[virtual] void QAbstractItemView:: keyboardSearch (const QString & search )

Moves to and selects the item best matching the string search . If no item is found nothing happens.

In the default implementation, the search is reset if search is empty, or the time interval since the last search has exceeded QApplication::keyboardInputInterval ().

QAbstractItemModel *QAbstractItemView:: model () const

Returns the model that this view is presenting.

另请参阅 setModel ().

[override virtual protected] void QAbstractItemView:: mouseDoubleClickEvent ( QMouseEvent * event )

重实现: QAbstractScrollArea::mouseDoubleClickEvent (QMouseEvent *e).

此函数被调用采用给定 event when a mouse button is double clicked inside the widget. If the double-click is on a valid item it emits the doubleClicked () signal and calls edit () on the item.

[override virtual protected] void QAbstractItemView:: mouseMoveEvent ( QMouseEvent * event )

重实现: QAbstractScrollArea::mouseMoveEvent (QMouseEvent *e).

此函数被调用采用给定 event when a mouse move event is sent to the widget. If a selection is in progress and new items are moved over the selection is extended; if a drag is in progress it is continued.

[override virtual protected] void QAbstractItemView:: mousePressEvent ( QMouseEvent * event )

重实现: QAbstractScrollArea::mousePressEvent (QMouseEvent *e).

此函数被调用采用给定 event when a mouse button is pressed while the cursor is inside the widget. If a valid item is pressed on it is made into the current item. This function emits the pressed () 信号。

[override virtual protected] void QAbstractItemView:: mouseReleaseEvent ( QMouseEvent * event )

重实现: QAbstractScrollArea::mouseReleaseEvent (QMouseEvent *e).

此函数被调用采用给定 event when a mouse button is released, after a mouse press event on the widget. If a user presses the mouse inside your widget and then drags the mouse to another location before releasing the mouse button, your widget receives the release event. The function will emit the clicked () signal if an item was being pressed.

[pure virtual protected] QModelIndex QAbstractItemView:: moveCursor ( QAbstractItemView::CursorAction cursorAction , Qt::KeyboardModifiers modifiers )

返回 QModelIndex 对象指向下一视图对象,基于给定 cursorAction and keyboard modifiers specified by modifiers .

在基类中,这是纯虚函数。

void QAbstractItemView:: openPersistentEditor (const QModelIndex & index )

Opens a persistent editor on the item at the given index . If no editor exists, the delegate will create a new editor.

另请参阅 closePersistentEditor () 和 isPersistentEditorOpen ().

[override virtual protected] void QAbstractItemView:: resizeEvent ( QResizeEvent * event )

重实现: QAbstractScrollArea::resizeEvent (QResizeEvent *event).

此函数被调用采用给定 event 当重置大小事件被发送给 Widget 时。

另请参阅 QWidget::resizeEvent ().

QModelIndex QAbstractItemView:: rootIndex () const

Returns the model index of the model's root item. The root item is the parent item to the view's toplevel items. The root can be invalid.

另请参阅 setRootIndex ().

[protected] void QAbstractItemView:: scheduleDelayedItemsLayout ()

Schedules a layout of the items in the view to be executed when the event processing starts.

Even if scheduleDelayedItemsLayout() is called multiple times before events are processed, the view will only do the layout once.

另请参阅 executeDelayedItemsLayout ().

[protected] void QAbstractItemView:: scrollDirtyRegion ( int dx , int dy )

Prepares the view for scrolling by ( dx , dy ) pixels by moving the dirty regions in the opposite direction. You only need to call this function if you are implementing a scrolling viewport in your view subclass.

若实现 scrollContentsBy () in a subclass of QAbstractItemView , call this function before you call QWidget::scroll () on the viewport. Alternatively, just call update ().

另请参阅 scrollContentsBy (), dirtyRegionOffset (),和 setDirtyRegion ().

[pure virtual] void QAbstractItemView:: scrollTo (const QModelIndex & index , QAbstractItemView::ScrollHint hint = EnsureVisible)

Scrolls the view if necessary to ensure that the item at index is visible. The view will try to position the item according to the given hint .

在基类中,这是纯虚函数。

[virtual protected] QModelIndexList QAbstractItemView:: selectedIndexes () const

This convenience function returns a list of all selected and non-hidden item indexes in the view. The list contains no duplicates, and is not sorted.

另请参阅 QItemSelectionModel::selectedIndexes ().

[virtual protected] QItemSelectionModel::SelectionFlags QAbstractItemView:: selectionCommand (const QModelIndex & index , const QEvent * event = nullptr) const

Returns the SelectionFlags to be used when updating a selection with to include the index specified. The event is a user input event, such as a mouse or keyboard event.

Reimplement this function to define your own selection behavior.

另请参阅 setSelection ().

QItemSelectionModel *QAbstractItemView:: selectionModel () const

返回当前选定模型。

另请参阅 setSelectionModel () 和 selectedIndexes ().

[protected] void QAbstractItemView:: setDirtyRegion (const QRegion & region )

Marks the given region as dirty and schedules it to be updated. You only need to call this function if you are implementing your own view subclass.

另请参阅 scrollDirtyRegion () 和 dirtyRegionOffset ().

void QAbstractItemView:: setIndexWidget (const QModelIndex & index , QWidget * widget )

设置给定 widget on the item at the given index , passing the ownership of the widget to the viewport.

index is invalid (e.g., if you pass the root index), this function will do nothing.

给定 widget 's autoFillBackground property must be set to true, otherwise the widget's background will be transparent, showing both the model data and the item at the given index .

If index widget A is replaced with index widget B, index widget A will be deleted. For example, in the code snippet below, the QLineEdit object will be deleted.

setIndexWidget(index, new QLineEdit);
...
setIndexWidget(index, new QTextEdit);
							

This function should only be used to display static content within the visible area corresponding to an item of data. If you want to display custom dynamic content or implement a custom editor widget, subclass QStyledItemDelegate 代替。

另请参阅 indexWidget () 和 委托类 .

void QAbstractItemView:: setItemDelegate ( QAbstractItemDelegate * delegate )

Sets the item delegate for this view and its model to delegate . This is useful if you want complete control over the editing and display of items.

任何现有委托将被移除,但不被删除。 QAbstractItemView 未拥有所有权对于 delegate .

警告: You should not share the same instance of a delegate between views. Doing so can cause incorrect or unintuitive editing behavior since each view connected to a given delegate may receive the closeEditor() 信号,并尝试访问、修改或关闭已关闭的编辑器。

另请参阅 itemDelegate ().

void QAbstractItemView:: setItemDelegateForColumn ( int column , QAbstractItemDelegate * delegate )

Sets the given item delegate used by this view and model for the given column . All items on column will be drawn and managed by delegate instead of using the default delegate (i.e., itemDelegate ()).

Any existing column delegate for column will be removed, but not deleted. QAbstractItemView 未拥有所有权对于 delegate .

注意: If a delegate has been assigned to both a row and a column, the row delegate will take precedence and manage the intersecting cell index.

警告: You should not share the same instance of a delegate between views. Doing so can cause incorrect or unintuitive editing behavior since each view connected to a given delegate may receive the closeEditor() 信号,并尝试访问、修改或关闭已关闭的编辑器。

另请参阅 itemDelegateForColumn (), setItemDelegateForRow (),和 itemDelegate ().

void QAbstractItemView:: setItemDelegateForRow ( int row , QAbstractItemDelegate * delegate )

Sets the given item delegate used by this view and model for the given row . All items on row will be drawn and managed by delegate instead of using the default delegate (i.e., itemDelegate ()).

Any existing row delegate for row will be removed, but not deleted. QAbstractItemView 未拥有所有权对于 delegate .

注意: If a delegate has been assigned to both a row and a column, the row delegate (i.e., this delegate) will take precedence and manage the intersecting cell index.

警告: You should not share the same instance of a delegate between views. Doing so can cause incorrect or unintuitive editing behavior since each view connected to a given delegate may receive the closeEditor() 信号,并尝试访问、修改或关闭已关闭的编辑器。

另请参阅 itemDelegateForRow (), setItemDelegateForColumn (),和 itemDelegate ().

[virtual] void QAbstractItemView:: setModel ( QAbstractItemModel * model )

设置 model for the view to present.

This function will create and set a new selection model, replacing any model that was previously set with setSelectionModel (). However, the old selection model will not be deleted as it may be shared between several views. We recommend that you delete the old selection model if it is no longer required. This is done with the following code:

QItemSelectionModel *m = view->selectionModel();
view->setModel(new model);
delete m;
							

If both the old model and the old selection model do not have parents, or if their parents are long-lived objects, it may be preferable to call their deleteLater () functions to explicitly delete them.

The view does not take ownership of the model unless it is the model's parent object because the model may be shared between many different views.

另请参阅 model (), selectionModel (),和 setSelectionModel ().

[pure virtual protected] void QAbstractItemView:: setSelection (const QRect & rect , QItemSelectionModel::SelectionFlags flags )

Applies the selection flags to the items in or touched by the rectangle, rect .

When implementing your own itemview setSelection should call selectionModel ()->select(selection, flags) where selection is either an empty QModelIndex QItemSelection that contains all items that are contained in rect .

另请参阅 selectionCommand () 和 selectedIndexes ().

[virtual] void QAbstractItemView:: setSelectionModel ( QItemSelectionModel * selectionModel )

Sets the current selection model to the given selectionModel .

Note that, if you call setModel () after this function, the given selectionModel will be replaced by one created by the view.

注意: It is up to the application to delete the old selection model if it is no longer needed; i.e., if it is not being used by other views. This will happen automatically when its parent object is deleted. However, if it does not have a parent, or if the parent is a long-lived object, it may be preferable to call its deleteLater () function to explicitly delete it.

另请参阅 selectionModel (), setModel (),和 clearSelection ().

[protected] void QAbstractItemView:: setState ( QAbstractItemView::State state )

将项视图的状态设为给定 state .

另请参阅 state ().

[virtual] int QAbstractItemView:: sizeHintForColumn ( int column ) const

Returns the width size hint for the specified column 或 -1 若没有模型。

This function is used in views with a horizontal header to find the size hint for a header section based on the contents of the given column .

另请参阅 sizeHintForRow ().

QSize QAbstractItemView:: sizeHintForIndex (const QModelIndex & index ) const

返回大小提示为项采用指定 index 或无效大小对于无效索引。

另请参阅 sizeHintForRow () 和 sizeHintForColumn ().

[virtual] int QAbstractItemView:: sizeHintForRow ( int row ) const

返回高度大小提示为指定 row 或 -1 若没有模型。

The returned height is calculated using the size hints of the given row 's items, i.e. the returned value is the maximum height among the items. Note that to control the height of a row, you must reimplement the QAbstractItemDelegate::sizeHint () 函数。

This function is used in views with a vertical header to find the size hint for a header section based on the contents of the given row .

另请参阅 sizeHintForColumn ().

[virtual protected] void QAbstractItemView:: startDrag ( Qt::DropActions supportedActions )

开始拖拽通过调用 drag->exec() 使用给定 supportedActions .

[protected] QAbstractItemView::State QAbstractItemView:: state () const

返回项视图的状态。

另请参阅 setState ().

[override virtual protected] void QAbstractItemView:: timerEvent ( QTimerEvent * event )

重实现: QObject::timerEvent (QTimerEvent *event).

此函数被调用采用给定 event 当计时器事件被发送给 Widget 时。

另请参阅 QObject::timerEvent ().

[pure virtual protected] int QAbstractItemView:: verticalOffset () const

返回视图的垂直偏移。

在基类中,这是纯虚函数。

另请参阅 horizontalOffset ().

[override virtual protected] bool QAbstractItemView:: viewportEvent ( QEvent * event )

重实现: QAbstractScrollArea::viewportEvent (QEvent *event).

此函数用于处理工具提示和 What's This? 模式,若给定 event QEvent::ToolTip ,或 QEvent::WhatsThis 。它将所有其它事件传递到其基类 viewportEvent() 处理程序。

返回 true if event 已被识别并处理;否则,返回 false .

[override virtual protected, since 5.2] QSize QAbstractItemView:: viewportSizeHint () const

重实现: QAbstractScrollArea::viewportSizeHint () const.

该函数在 Qt 5.2 引入。

[pure virtual] QRect QAbstractItemView:: visualRect (const QModelIndex & index ) const

返回项所占据的视口矩形在 index .

若项显示在多个区域,那么 visualRect 应返回包含索引的首要区域,而不是索引可能环绕、触摸或导致绘制的完整区域。

在基类中,这是纯虚函数。

另请参阅 indexAt () 和 visualRegionForSelection ().

[pure virtual protected] QRegion QAbstractItemView:: visualRegionForSelection (const QItemSelection & selection ) const

返回来自项视口的区域在给定 selection .

在基类中,这是纯虚函数。

另请参阅 visualRect () 和 selectedIndexes ().