28、视图基类 QAbstractItemView---------Model/View模型视图

视图基类 QAbstractItemView

视图类是控制数据显示的,同样有一个纯虚类QAbstractitemView, 它的方法大家只做理解即可,不用死记硬背,后面我们会根据具体的子类和案例带着大家学会这些操作。

●设置和返回模型

cpp 复制代码
void setModel(QAbstractItemModel *model) //设置数据模型
QAbstractItemModel *model() //返回关联的数据模型对象指针
  • 获取选中的模型
cpp 复制代码
QItemSelectionModel *selectionModel() const;
  • 设置代理
cpp 复制代码
// 设置代理
void  setItemDelegate(QAbstractItemDelegate *delegate);
// 为某一列设置代理
void  setItemDelegateForColumn(int column, QAbstractItemDelegate *delegate)
// 为某一行设置代理
void  setItemDelegateForRow(int row, QAbstractItemDelegate *delegate)
  • 设置属性和获取属性
cpp 复制代码
void setEditTriggers(QAbstractItemView::EditTriggers triggers) 
QAbstractItemView::EditTriggers editTriggers() 

属性值的类型为该属性值是标志类型 QAbstractItemView::EditTriggers, 它可以是几种枚举类型的并集

cpp 复制代码
- NoEditTriggers:不允许编辑。
- CurrentChanged:当前项变化时进入编辑状态。
- DoubleClicked:双击一个项时进入编辑状态。
- SelectedClicked:点击一个已选择的项时进入编辑状态。
- EditKeyPressed:当平台的编辑按键被按下时进入编辑状态

视图组件类和模型类都没有 readonly 属性,如果要设置数据是只读的,用函数 setEditTriggers()

设置视图组件为不允许编辑即可。

  • 设置选择模式
cpp 复制代码
void  setSelectionMode(QAbstractItemView::SelectionMode mode);

选择模式包括以下

cpp 复制代码
SingleSelection:    单选,只能选择一个项,例如只能选择一个单元格。
ContiguousSelection:连续选择,例如按住 Shift 键选择多个连续单元格。
ExtendedSelection:  扩展选择,例如可以按住 Ctrl 键选择多个不连续的单元格。
MultiSelection:     多选,例如通过拖动鼠标选择多个单元格。
NoSelection:        不允许选择
  • 设置选择模式有时我们可以设置选择整行或者多行
cpp 复制代码
void setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)

选择模式包括

cpp 复制代码
QAbstractItemView::SelectItems:  选择任意单元格
QAbstractItemView::SelectRows:   整行选择
QAbstractItemView::SelectColumns: 整列选择
相关推荐
xcyxiner3 小时前
DicomViewer (后台线程处理文件)4
qt
xcyxiner10 小时前
DicomViewer (添加模型类)3
qt
xcyxiner1 天前
DicomViewer (目录调整) 2
qt
xcyxiner1 天前
dcmtk vtk vtk-dicom(gdcm) 编译(debug) v2
qt
clint4562 天前
C++进阶(1)——前景提要
c++
夜悊2 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴2 天前
CMake 021: IF 条件判据详诠
c++·cmake
_wyt0013 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp
玖玥拾3 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
桥田智能3 天前
桥田智能 QT-650S:面向白车身焊装的 800kg 重载快换解决方案
开发语言·qt·系统架构