QAbstractItemView
描述:
参考:
链接:https://pan.baidu.com/s/1iD68j6gSHCIddEFwZCK6ew
提取码:e6r0
QAbstractItemView 为所有的视图项提供了基础的功能。
QAbstractItemView是所有的使用QAbstractItemModel模型的视图的基类,是一个不能被实例化的抽象类。它通过信号槽机制为与模型的交互操作提供了一个标准化的接口,确保子类视图能够随着模型的变化而及时更新。该类对键盘和鼠标的导航、视窗的滚动、项的编辑以及选择提供了标准的支持。键盘导航实现了如下的功能:
Keys | Functionality |
Arrow keys | 改变当前的项并且选中它 |
Ctrl+Arrow keys | 改变当前的项但是不选中它 |
Shift+Arrow keys | 改变当前的项并且选中它 而且之前选中的项不会被取消选中 |
Ctr+Space | Toggles selection of the current item. |
Tab/Backtab | 讲当前项设置为前一个或者后一个。 |
Home/End | 选择模型中的第一个或者最后一个项。 |
Page up/Page down | 滚动当前的项到在原来视图不可见的下一页或者上一页 |
Ctrl+A | 选择所有的项 |
(注意:上表是在假设selection mode 允许操作的基础上的,比如说 如果selection mode是 QAbstractItemView::NoSelection的时候 上表就失效了。)
当视图项继承自QAbstractItemView时 只需要实现他们需要的特定的功能就行了。
To make sure that an item is visible use scrollTo().
QAbstractItemView 的一些函数关注于滚动上,比如 setHorizontalScrollMode() 和 setVerticalScrollMode() ;另外一些函数关注与选择模式,比如说 setSelectionMode(), 和setSelectionBehavior()。
为了完全的控制项的显示和编辑,可以通过 setItemDelegate().函数设定一个委托。
注意:当一个继承自 QAbstractItemView的对象想要更新视口里的内容时 应该使用 viewport->update() 而不是update()函数 因为所有的绘制操作都发生在视口中。
属性:
enum QAbstractItemView::CursorAction
该属性描述了不同项之间的导航方式。
Constant | Value | Description |
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 | 将项移动到当前项之前 |
enum QAbstractItemView::DragDropMode
该属性包含了项能对dragEvent 和dropEvent做出的回应。默认情况下是不能回应(NoDragDrop)。
Constant | Value | Description |
QAbstractItemView::NoDragDrop | 0 | 不支持拖拽和放置。. |
QAbstractItemView::DragOnly | 1 | 只支持拖拽 |
QAbstractItemView::DropOnly | 2 | 只接受放置 |
QAbstractItemView::DragDrop | 3 | 支持拖拽和放下 |
QAbstractItemView::InternalMove | 4 | 只接受自己的移动(不是复制) |
enum QAbstractItemView::DropIndicatorPosition
该属性描述了放置指示器和当前鼠标位置的关系。
Constant | Value | Description |
QAbstractItemView::OnItem | 0 | 项被放置在index处 |
QAbstractItemView::AboveItem | 1 | 项被放在index之上 |
QAbstractItemView::BelowItem | 2 | 项被放置index之下 |
QAbstractItemView::OnViewport | 3 | 项被放置到视口中没有项的区域。. 被放置的区域决定于视口采用的模型。 |
enum QAbstractItemView::EditTrigger
该属性描述了什么action会启动项编辑
Constant | Value | Description |
QAbstractItemView::NoEditTriggers | 0 | 不启动编辑 |
QAbstractItemView::CurrentChanged | 1 | 当当前项改变时启动编辑。 |
QAbstractItemView::DoubleClicked | 2 | 当项被双击时启动编辑 |
QAbstractItemView::SelectedClicked | 4 | 点击已选中的项是启动编辑 |
QAbstractItemView::EditKeyPressed | 8 | Editing starts when the platform edit key has been pressed over an item. |
QAbstractItemView::AnyKeyPressed | 16 | 有按键在item上按下时启动编辑 |
QAbstractItemView::AllEditTriggers | 31 | 上述所有的信号都会触发 |
enum QAbstractItemView::ScrollHint
Constant | Value | Description |
QAbstractItemView::EnsureVisible | 0 | 确保滚动后项可见 |
QAbstractItemView::PositionAtTop | 1 | 确保滚动后项在视口的顶部. |
QAbstractItemView::PositionAtBottom | 2 | 确保滚动后项在视口的底部 |
QAbstractItemView::PositionAtCenter | 3 | 确保滚动后项在视口的中心 |
enum QAbstractItemView::ScrollMode
Constant | Value | Description |
QAbstractItemView::ScrollPerItem | 0 | 一次滚动一项 |
QAbstractItemView::ScrollPerPixel | 1 | 一次滚动一个像素 |
enum QAbstractItemView::SelectionBehavior
Constant | Value | Description |
QAbstractItemView::SelectItems | 0 | 选择单个项. |
QAbstractItemView::SelectRows | 1 | 选择单行 |
QAbstractItemView::SelectColumns | 2 | 选择单列 |
enum QAbstractItemView::SelectionMode
Constant | Value | Description |
QAbstractItemView::SingleSelection | 1 | 当用户选中一个项之后,之前选中的项全部被取消选中,而且用户也不能通过点击而取消这个项的被选中状态。 |
QAbstractItemView::ContiguousSelection | 4 | 当用户点击一个新的项时,以前被选中的项取消选中状态。如果用户按下shift键时,当前项和所有项之间的所有项都会被选中(或者全部取消选中) |
QAbstractItemView::ExtendedSelection | 3 | 当用户点击一个新的项时,以前被选中的项取消选中状态。 当用户点击时按住Ctrl键,就可以一次选择多个项。如果用户按下shift键时,当前项和所有项之间的所有项都会被选中(或者全部取消选中)多个项也可以通过鼠标的拖拽选中
|
QAbstractItemView::MultiSelection | 2 | 当用户点击时按住Ctrl键,就可以一次选择多个项。 |
QAbstractItemView::NoSelection | 0 | 所有的项都不能选中。 |
enum QAbstractItemView::State
该属性描述了项的不同的状态。
Constant | Value | Description |
QAbstractItemView::NoState | 0 | T默认的状态 |
QAbstractItemView::DraggingState | 1 | 用户拖拽项时的状态。 |
QAbstractItemView::DragSelectingState | 2 | 用户选中项时的状态 |
QAbstractItemView::EditingState | 3 | 用户在widget中编辑项时的状态 |
QAbstractItemView::ExpandingState | 4 | 用户打开项的分支时的状态 |
QAbstractItemView::CollapsingState | 5 | 用户关闭项的分支时的状态 |
QAbstractItemView::AnimatingState | 6 | 项处于动画时的状态 |
alternatingRowColors : bool
该属性描述了是否对背景应用交替的颜色。
如果该属性是true的话 ,就使用 QPalette::Base 和 QPalette::AlternateBase交替描绘。默认情况下是false。
autoScroll : bool
该属性描述了当鼠标在视图的边缘拖拽时 项是否会自动滚动。
autoScrollMargin : int
该属性描述了鼠标在视图边缘为该值得时候拖拽会使视口滚动,默认值为16像素。
defaultDropAction : Qt::DropAction
该属性描述了QAbstractItemView::drag().函数默认采用的放置动作。当该属性不设置时,默认的动作是CopyAction。
dragDropMode : DragDropMode
该属性描述了当发生拖拽和放置动作的时候视窗被触发的事件。
dragDropOverwriteMode : bool
该属性描述了拖拽放置时项的行为。当该值为true时,拖拽放置后,被选中的数据会覆盖原有的项的数据,移动数据时会消除项。当该值为false(默认值)时,选中的数据会作为一个新项被插入,当数据被移除时,该插入的项被移除。(我理解的就是文件夹里面,文件本身不是视图,但是放置文件的地方就是视图(文件夹同理))
dragEnabled : bool
该属性包含了该视图内的项是否支持拖拽。
editTriggers : EditTriggers
该属性包含了哪个动作会启动项的编辑。能用“或”操作符多选。
horizontalScrollMode : ScrollMode
该属性包含了视图怎么在水平方向滚动(以像素为单位还是一项为单位)
iconSize : QSize
该属性包含了项图标的大小。(当项可见时 设置这个值会造成项的重新布局)
selectionBehavior : SelectionBehavior
该属性包含了视图采用的选择模式,选中项还是整行或者整列。
selectionMode : SelectionMode
该属性包含了视图选择时是支持单选 多选 连续选 不选等的属性。
showDropIndicator : bool
该属性包含了当拖拽和放置的时候是否采用指示器。
tabKeyNavigation : bool
该属性包含了是否支持tab键和backtab(shift+tab)的导航。
textElideMode : Qt::TextElideMode
This property holds the position of the "..." in elided text.
The default value for all item views is Qt::ElideRight.
verticalScrollMode : ScrollMode
该属性包含了视图怎么在垂直方向滚动(以像素为单位还是一项为单位)
函数:
void QAbstractItemView::closePersistentEditor(const QModelIndex & index)
Closes the persistent editor for the item at the given index.
关闭index指定的项的编辑器。
QModelIndex QAbstractItemView::currentIndex() const
返回当前项的index
QPoint QAbstractItemView::dirtyRegionOffset() const
Returns the offset of the dirty regions in the view.
bool QAbstractItemView::edit(const QModelIndex & index, EditTrigger trigger, QEvent * event)(虚保护函数)
按照index指定的项编辑项,如果有需要的话建立一个编辑器,当视口的的状态是EditingState,则返回true,否则返回false。
Trigger描述了造成编辑进程的动作,可以将其写为QAbstractItemView::AllEditTriggers.从而强转编辑。
形参中的event指定了编辑相关的event
void QAbstractItemView::executeDelayedItemsLayout()
在事件进程之前执行设定好的布局。
int QAbstractItemView::horizontalOffset() const
返回视口水平方向的偏移
QModelIndex QAbstractItemView::indexAt(const QPoint & point) const(纯虚函数)
返回point处的ModelIndex。
QWidget * QAbstractItemView::indexWidget(const QModelIndex & index) const
返回给定的index处的控件。
bool QAbstractItemView::isIndexHidden(const QModelIndex & index) const(纯虚函数)
如果给定的index是隐藏的则返回true 否则返回false
QAbstractItemDelegate * QAbstractItemView::itemDelegate() const
返回视图和模型使用的项代理,返回值是setItemDelegate()设定的,或者是默认值。
QAbstractItemDelegate * QAbstractItemView::itemDelegate(const QModelIndex & index) const
返回视图和模型在当前项中使用的项代理
QAbstractItemDelegate * QAbstractItemView::itemDelegateForColumn(int column) const
返回视图和模型在当前列使用的项代理
QAbstractItemDelegate * QAbstractItemView::itemDelegateForRow(int row) const
返回视图和模型在当前行使用的项代理
void QAbstractItemView::keyboardSearch(const QString & search)
在视图中按照形参中的search搜索item。
QAbstractItemModel * QAbstractItemView::model() const
返回view展现的model
QModelIndex QAbstractItemView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)(纯虚函数)
该函数基于形参中的cursorAction,和modifiers 返回在视图中指向下一个对象的QModelIndex,
void QAbstractItemView::openPersistentEditor(const QModelIndex & index)
按照形参中的index为item打开一个持久的编辑器,如果没有编辑器的话,则通过委托创建一个。
QModelIndex QAbstractItemView::rootIndex() const
Returns the model index of the model's root item
返回模型的根的项的index
void QAbstractItemView::scheduleDelayedItemsLayout()
设立一个事件进程开始时采用的item的布局。即使scheduleDelayedItemsLayout()函数在事件进程之前被调用很多次,这个视图还是只会执行一次布局。
void QAbstractItemView::scrollDirtyRegion(int dx, int dy)
将边界区域向相反方向移动(dx,dy个像素的距离),一般在应用滚动的视口的视图子类中才会调用这个函数。如果在视图的子类中实现了scrollContentsBy()函数,那么会在调用scroll()函数之前调用本函数,而且要交替的调用update()函数。
void QAbstractItemView::scrollTo(const QModelIndex & index, ScrollHint hint = EnsureVisible)(纯虚函数)
滚动视图以确保index对应的项可见,根据形参中的枚举变量hint去设置该项的位置。
QModelIndexList QAbstractItemView::selectedIndexes() const(虚保护)
返回选中的项的index的一个链表。
QItemSelectionModel::SelectionFlags QAbstractItemView::selectionCommand(const QModelIndex & index, const QEvent * event = 0) const(虚保护)
返回当更新一个index指定的选项时使用的SelectionFlags ,形参中的Event指的是一些输入的事件,比如说鼠标和键盘事件。
QItemSelectionModel * QAbstractItemView::selectionModel() const
返回当前选项的模型。
void QAbstractItemView::setDirtyRegion(const QRegion & region)
设形参的region为边界区域。
void QAbstractItemView::setIndexWidget(const QModelIndex & index, QWidget * widget)
该函数将index处的项设置为形参的widget,然后这个widget被视口所拥有。注意,形参中的widget的autoFillBackground属性必须为true,否则widget的背景颜色为透明;还有一点,如果两次给同一个index指定widget,那么后面指定的有效,前面指定的被删除(注意,不是被覆盖,是被删除)。
void QAbstractItemView::setItemDelegate(QAbstractItemDelegate * delegate)
将delegate设立为项的视图和模型的委托,当想要去控制项的编辑和显示时,该功能非常有用。Delegate并不归QAbstractItemView所有,相当于借用。
注意:两个不同的视图之间不应该共享一个实例化的delegate,会发生一些错误。
void QAbstractItemView::setItemDelegateForColumn(int column, QAbstractItemDelegate * delegate)
将delegate设立为项指定行的视图和模型的委托,当想要去控制项的编辑和显示时,该功能非常有用。Delegate并不归QAbstractItemView所有,相当于借用。
void QAbstractItemView::setItemDelegateForRow(int row, QAbstractItemDelegate * delegate)
将delegate设立为项指定列的视图和模型的委托,当想要去控制项的编辑和显示时,该功能非常有用。Delegate并不归QAbstractItemView所有,相当于借用。
[virtual] void QAbstractItemView::setModel(QAbstractItemModel * model)
Sets the model for the view to present.
将形参中的model设定为视图展示的模型。
void QAbstractItemView::setSelection(const QRect & rect, QItemSelectionModel::SelectionFlags flags)
设置rect内所有的项的SelectionFlags为flags。
void QAbstractItemView::setSelectionModel(QItemSelectionModel * selectionModel)设置selectionModel为当前的选择模型。
void QAbstractItemView::setState(State state)
将项的状态设定为指定的状态。
int QAbstractItemView::sizeHintForColumn(int column) const(虚函数)
返回指定列的初始化宽度。
QSize QAbstractItemView::sizeHintForIndex(const QModelIndex & index) const
返回指定项的size。
int QAbstractItemView::sizeHintForRow(int row) const(虚函数)
返回指定行的初始化宽度。
void QAbstractItemView::startDrag(Qt::DropActions supportedActions)
用给定的supportedActions方式执行drag操作。
State QAbstractItemView::state() const
返回项的视图状态。
int QAbstractItemView::verticalOffset() const
返回视口垂直方向的偏移
QRect QAbstractItemView::visualRect(const QModelIndex & index) const(纯虚函数)
返回给定的项所在的矩形(不包括它的边界)。
QRegion QAbstractItemView::visualRegionForSelection(const QItemSelection & selection) const
返回给定的项在视口中的区域。
信号
void QAbstractItemView::activated(const QModelIndex & index)
该信号当有项被选中时发送。如何选中跟平台有关,比如说单机或者双击 或者按住enter键。
void QAbstractItemView::clicked(const QModelIndex & index)
该信号是当鼠标左键单击项的时候发送。
void QAbstractItemView::doubleClicked(const QModelIndex & index)
该信号是当鼠标左键双击项的时候发送。
DropIndicatorPosition QAbstractItemView::dropIndicatorPosition() const
Returns the position of the drop indicator in relation to the closest item.
返回最近项放置指示器的DropIndicatorPosition。
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.
该信号是当鼠标进去index指定的项时被触发
void QAbstractItemView::pressed(const QModelIndex & index)
当指定的item被鼠标点击时触发。
槽函数:
void QAbstractItemView::clearSelection()
所有的选中项都会被取消选中,当前的index不变。
void QAbstractItemView::closeEditor(QWidget * editor,
QAbstractItemDelegate::EndEditHint hint)
(虚保护类型函数)
关闭给定的编辑器并且释放它。
形参中的hint是去指定 当编辑操作结束的时候视图做出什么回应。
void QAbstractItemView::commitData(QWidget * editor)(虚保护函数)
Commit the data in the editor to the model.
将形参中editor里的数据返回给model(MVC中的model部分,它负责数据库的存取)
void QAbstractItemView::currentChanged(const QModelIndex & current, const QModelIndex & previous)(虚保护函数)
该函数是当一个新的item变成当前item时被调用的。
void QAbstractItemView::dataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight, const QVector<int> & roles = QVector<int> ())(虚保护函数)
该函数是当给定的范围内(从形参中的左上角到右下角的范围内)model的项发生变化时调用。
void QAbstractItemView::edit(const QModelIndex & index)
丐函数是当index对应的项可编辑时调用。用户有时候会发现tab导航无法正常工作,因为当前的index确定前一个和后一个被编译的项,所以一般会调用setCurrentIndex()函数将形参中的index设置为当前index。
void QAbstractItemView::editorDestroyed(QObject * editor)
当编辑器被摧毁时调用该函数。
void QAbstractItemView::reset()(虚保护)
重设视图的内部状态。.
void QAbstractItemView::rowsAboutToBeRemoved(const QModelIndex & parent, int start, int end)(虚保护)
这个函数当某些行被移除时调用。这个行是形参中的parent下的从start到end中的所有行。
void QAbstractItemView::rowsInserted(const QModelIndex & parent, int start, int end)(虚保护)
这个函数当某些行被插入时调用。这些行是形参中的parent下的从start到end中的所有行。
void QAbstractItemView::scrollToBottom()
去底部。
void QAbstractItemView::scrollToTop()
返回顶部。
void QAbstractItemView::selectAll()(虚函数)
全选
void QAbstractItemView::selectionChanged(const QItemSelection & selected, const QItemSelection & deselected)(虚保护)
这个槽函数是当选则项改变时调用。之前选中项和之后选中项分别由形参指定。
void QAbstractItemView::setCurrentIndex(const QModelIndex & index)
将index对应的项设置为当前项。
void QAbstractItemView::setRootIndex(const QModelIndex & index)
将index对应的项设定为根项。(虚函数)
void QAbstractItemView::update(const QModelIndex & index)(虚保护)
Updates the area occupied by the given index
更新被index指定的的项占据的区域。
void QAbstractItemView::viewportEntered()
当鼠标光标进入视口的时候该信号被发送。(鼠标追踪的值应该设立为true,不然没法工作)