QAbstractItemView类参考

https://blog.csdn.net/u010002704/article/details/41157963[原博客]

QAbstractItemView类参考

QAbstractItemView类提供项目视图类的基本功能。更多

 #include< QAbstractItemView >
    
    

qmake: QT   +=   widgets

继承自:QAbstractScrollArea

被继承:QColumnViewQHeaderViewQListView,QTableView,和QTreeView

公共类型

enum DragDropMode { NoDragDrop, DragOnly, DropOnly, DragDrop, InternalMove }
enum EditTrigger { NoEditTriggers, CurrentChanged, DoubleClicked, SelectedClicked, ..., 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 = 0)
  ~QAbstractItemView()
bool alternatingRowColors() const
int autoScrollMargin() const
void closePersistentEditor(const QModelIndex & index)
QModelIndex currentIndex() const
Qt::DropAction defaultDropAction() const
DragDropMode dragDropMode() const
bool dragDropOverwriteMode() const
bool dragEnabled() const
EditTriggers editTriggers() const
bool hasAutoScroll() const
ScrollMode horizontalScrollMode() const
QSize iconSize() const
virtual QModelIndex indexAt(const QPoint & point) const = 0
QWidget * indexWidget(const QModelIndex & index) const
QAbstractItemDelegate * itemDelegate() const
QAbstractItemDelegate * itemDelegate(const QModelIndex & index) const
QAbstractItemDelegate * itemDelegateForColumn(int column) const
QAbstractItemDelegate * itemDelegateForRow(int row) const
virtual void keyboardSearch(const QString & search)
QAbstractItemModel * model() const
void openPersistentEditor(const QModelIndex & index)
QModelIndex rootIndex() const
virtual void scrollTo(const QModelIndex & index, 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(DragDropMode behavior)
void setDragDropOverwriteMode(bool overwrite)
void setDragEnabled(bool enable)
void setDropIndicatorShown(bool enable)
void setEditTriggers(EditTriggers triggers)
void setHorizontalScrollMode(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(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
ScrollMode verticalScrollMode() const
virtual QRect visualRect(const QModelIndex & index) const = 0


重载的公有函数

virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const

公共槽

信号

受保护的类型


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

受保护的函数

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



重载的保护函数

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

保护槽

  • 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 QVector<int> &roles = QVector<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()

  • 1个保护槽继承自QWidget
额外的继承成员
  • 1 个公有变量继承自 QObject
  • 5 个公有的静态成员继承自 QWidget
  • 10 个公有的静态成员继承自 QObject
  • 2 个受保护的变量继承自 QObject
  • 1 个受保护的类型继承自 QPaintDevice

详细说明

QAbstractItemView类提供项目视图类的基本功能。

QAbstractItemView类是一个基类,它为每个标准视图,使用QAbstractItemModel。QAbstractItemView是一个抽象类并且本身不能被实例化。它和模型通过信号和槽机制为互操作提供了一个标准接口,使子类来保持最新的与他们的模型的变化。该类提供了键盘和鼠标导航,视图滚动,编辑项目和选择的标准支持。实现这项功能的键盘导航:

钥匙 功能
箭头键 改变当前的项目和选择。
按Ctrl +箭头键 改变现行的项目而不选择它。
Shift +箭头键 改变当前的项目和选择。以前选择的项目(S)不取消。
CTR +空格 当前项目的切换选择。
Tab/Backtab 更改当前项目下/上一个项目。
Home/End 选择模型中的第一/最后的项目。
Page up/Page down 向上/下滚动视图中的可见行
Ctrl + A 选择模型中的所有项目。

注意:上表假设选择模式允许的操作。例如,如果选择模式为QAbstractItemView::NoSelection,你不能选择项目。

QAbstractItemView类是模型/视图类模型/视图架构的一部分

视图类继承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()),但这可以通过使用一个QItemSelectionModel实例调用setSelectionModel()被替换。

为了完全控制项的显示和编辑,你可以用setItemDelegate()指定一个代理。

QAbstractItemView提供了大量的保护函数。一些和编辑有关的函数如edit(),commitData().而其他的是键盘和鼠标的事件处理。

注意:如果你继承QAbstractItemView并打算更新视口的内容,你应该用viewport->update()而不是让所有的绘制操作发生在视口的update()。

也可查看 View ClassesModel/View ProgrammingQAbstractItemModel, and Chart Example.

如果旧的模式和旧的选择模型没有父母,或如果他们的父母长寿的对象,它可能是最好打电话给他们deletelater()函数来删除它。

视图不除非它的父对象的模型,因为模型可能有许多不同的视图之间共享将模型的所有权。

参见模型(),选择模式(),和setselectionmodel()。

成员类型文档

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 移动到当前项的前一个项

也可查看 moveCursor().

enum QAbstractItemView::DragDropMode

描述了各种视图可以采用的拖放事件。默认情况下,视图不支持拖放(NoDragDrop)。

Constant Value Description
QAbstractItemView::NoDragDrop 0 不支持拖放
QAbstractItemView::DragOnly 1 仅支持视图项的拖动
QAbstractItemView::DropOnly 2 接受放下(drop)
QAbstractItemView::DragDrop 3 视图支持拖放
QAbstractItemView::InternalMove 4 视图仅支持来自自身的移动操作(不复制)
请注意,使用的模型需要提供拖放操作的支持。

这个枚举类型在Qt4.2被引进或修改。

参见setDragDropMode()和在项视图中使用拖放。

enum QAbstractItemView::DropIndicatorPosition

这个枚举类型标明了和当前鼠标位置相关的索引的按下位置

Constant Value Description
QAbstractItemView::OnItem 0 The item will be dropped on the index.
QAbstractItemView::AboveItem 1 The item will be dropped above the index.
QAbstractItemView::BelowItem 2 The item will be dropped below the index.
QAbstractItemView::OnViewport 3 The 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


flags QAbstractItemView::EditTriggers

此枚举描述了将要开始的项编辑的动作。

Constant Value Description
QAbstractItemView::NoEditTriggers 0 No editing possible.
QAbstractItemView::CurrentChanged 1 只要当前项改变,编辑开始
QAbstractItemView::DoubleClicked 2 当一个项被双击时编辑开始
QAbstractItemView::SelectedClicked 4 在已选择项上单击时编辑开始
QAbstractItemView::EditKeyPressed 8 在项上当编辑键被按下时编辑开始
QAbstractItemView::AnyKeyPressed 16 在项上任何键被按下编辑开始
QAbstractItemView::AllEditTriggers 31 上述所有触发编辑开始
EditTriggers类型是QFlags<EditTrigger>的typedef,它存储了EditTrigger值的一个或操作。

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 视图将滚动内容一次一像素
这个枚举在Qt4.2被引进或修改。

enum QAbstractItemView::SelectionBehavior

Constant Value Description
QAbstractItemView::SelectItems 0 选择项
QAbstractItemView::SelectRows 1 仅选择行
QAbstractItemView::SelectColumns 2 仅选择列

enum QAbstractItemView::SelectionMode

This enum indicates how the view responds to user selections:

Constant Value Description
QAbstractItemView::SingleSelection 1 当用户选择一个项目时,任何已选定的项目成为未选中状态,并且用户不能通过单击已选择的项来取消选择。
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.按下Shift可多选或取消选择
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.按下Ctrl可多选或取消选择
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

描述了视图可处于的不同状态。 当重写你自己的视图时这通常是有趣的。

Constant Value Description
QAbstractItemView::NoState 0 默认
QAbstractItemView::DraggingState 1 用户正在拖动项
QAbstractItemView::DragSelectingState 2 用户正在选择项
QAbstractItemView::EditingState 3 用户在一个可编辑控件中编辑项
QAbstractItemView::ExpandingState 4 用户正打开一个项的分支
QAbstractItemView::CollapsingState 5 用户正关闭一个项的分支
QAbstractItemView::AnimatingState 6 项视图正在进行动画

属性文档

alternatingRowColorsbool

这个属性保存的是是否用可交替颜色去绘制背景。

如果此属性为真,项目背景将使用QPalette::baseQPalette::AlternateBase绘制;否则将用QPalette::Base绘制背景。

默认情况下,此属性为false

访问函数:

bool alternatingRowColors() const
void setAlternatingRowColors(bool enable)

autoScroll : bool

这个属性判断当拖动移动事件可用时是否自动滚动。

如果此属性设置为True(默认的),QAbstractItemView自动滚动视图的内容,当用户在视口边缘的16像素内拖动时。如果当前项目改变了,视图将自动滚动以确保当前项完全可见。

此属性仅在视口接受drop时可用。设置为false关闭此属性。

访问功能:

Access functions:

bool hasAutoScroll() const
void setAutoScroll(bool enable)

autoScrollMargin : int

这个属性保存的是面积的大小,当自动滚动触发。

此属性可以控制面积的大小在边缘的视图,触发器autoscrolling。默认值是16像素。

此属性进行了Qt 4.4。

访问函数:

bool hasAutoScroll() const
void setAutoScroll(bool enable)

autoScrollMargin : int

此属性持有当自动滚动被触发时的区域的大小。

此属性控制了触发自动滚动时视口边缘区域的尺寸。默认值是16像素。

此属性在Qt4.4被引进。

访问函数:

int autoScrollMargin() const
void setAutoScrollMargin(int margin)

defaultDropAction : Qt::DropAction

这个属性持有将在QAbstractItemView::drag()中被用到的默认(鼠标拖动)松开动作。

如果此属性没被设置,松开动作是CopyAction,如果可支持的动作支持CopyAction的话。

此属性在Qt4.6被引进。

访问函数:

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

也可查看 showDropIndicator 和 dragDropOverwriteMode.

dragDropMode : DragDropMode

这个属性保存视图将采用的拖放事件。

此属性被引进始自Qt 4.2。

访问函数:

DragDropMode dragDropMode() const
void setDragDropMode(DragDropMode behavior)

也可查看 showDropIndicator 和 dragDropOverwriteMode.

dragDropOverwriteMode : bool

这个属性保存的视图的拖放行为。

如果它的值是真的,当drop时选择的数据将覆盖现有的项目数据,而移动数据将会清除项目。如果它的值是false,当数据被drop时选定的数据将作为一个新的项被插入。当数据被移动,该项目也被除去。

QListViewQTreeView子类中默认设置为false。然而在QTableView子类中,该属性已设置为true

注:这并不打算防止重写项,模型的flags()的实现应该不返回QT::itemIsDropEnabled

此属性始自Qt 4.2。

访问函数:

bool dragDropOverwriteMode() const
void setDragDropOverwriteMode(bool overwrite)

也可查看 dragDropMode.

dragEnabled : bool

这个属性判断视图是否支持项的拖动

访问函数:

bool dragEnabled() const
void setDragEnabled(bool enable)

也可查看 showDropIndicatorDragDropModedragDropOverwriteMode, and acceptDrops.

editTriggers : EditTriggers

这个属性保存哪一个动作将启动项编辑。

此属性是一个被EditTrigger定义的标志选择,结合or操作。视图仅在动作在此属性中设置了的时候启动一个项的编辑

可使用函数:

EditTriggers editTriggers() const
void setEditTriggers(EditTriggers triggers)

horizontalScrollMode : ScrollMode

这个属性保存的是视图如何在水平方向上滚动内容。

此属性可以控制视图如何水平滚动其内容。滚动可以每像素或每项。

此属性始自Qt 4.2。

访问函数:

ScrollMode horizontalScrollMode() const
void setHorizontalScrollMode(ScrollMode mode)

iconSize : QSize

这个属性保存项图标的大小。

视图可见时设置此属性将造成项再次被布局。

访问函数:

QSize iconSize() const
void
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值