QT学习系列:模型/视图框架

最近接触的项目中接触到了模型/视图框架,到网上找了一些材料,看的一知半解,把自己学到的一点东西以自己的理解做下记录

模型视图框架有三个很重要的概念

模型model:和数据打交道,用来组织数据

视图view:和用户打交道,通过联系指定的模型,将数据以特定格式显示给用户

委托delegate:用于控制用户交互的格式

从网上盗用一个图:


模型

所有的模型都基于QAbstractItemModel类

QT提供了一些现成的模型,用来处理数据:

QStringListModel:用来处理简单的QString列表项

QStandardItemModel:管理更复杂的树结构件,其中每一个项目可以包含任意数据

QFileSystemModel:提供有关本地文件系统的文件和目录信息

QSqlQueryModel、QSqlTableModel、QSqlRelationalTableModel:使用模型/视图约定来访问数据库。

如果这些标准模型不能满足要求,则可以继承化QAbstractItemModel、QAbstractListModel或QAbstractTableModel来创建自定义模型

视图

根据视图结构不同QT实现了三种视图类型:

QListView:简单的以列表形式显示数据

QTableView:以表格的形式显示数据

QTreeView:以分层次的树形结构显示数据

以上三个类都继承自QAbstractItemView类,如果以上三种视图满足不了要求,也可以自己继承实现

委托

讲真,委托这个我没有搞太明白,只是知道是控制用户交互方式的,比如说,一个表格中的单元格,你想更改其中的数据,

是输入字符串,还是有下拉框可以选择,亦或者是弹出时间框进行选择。就这样

QAbstractItemDelegate是委托的抽象基类,一般实现委托功能用一下方式

class MyStyleItemDelegate : public QStyleItemDelegate

{

    MyStyleItemDelegate();    // 构造

    // 当用户想要修改视图,这时候视图需要提供一个编辑器,视图会告知委托为被修改的项目提供一个编辑器部件

    // 此函数就是委托为视图提供编辑器的

    QWidget *createEdit(QWidget *parent, const QStyleOptionViewItem &option,

                                        const QModelIndex &index) const;

    // 为编辑器设置数据

    void setEditorData(QWidget *editor, const QModelIndex &index) const;

    // 将数据写入到模型

    void setModelData(QWidget *editor, QAbstractItemModel *model,

                                    const QModelIndex &index) const;

    // 更新编辑器的几何布局

    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,

                                                    const QModelIndex &index) const;

}

模型/视图框架的便捷类

QT提供了一些便捷类来使用模型/视图框架,所谓的简便类,其实是把模型和视图两个部件集成在了一起

QListWidget:以列表的形式显示数据

QTabWidget:以表格的形式显示数据

QTreeWidget:以树的形式显示数据

以下记录一些工作中用到的函数:

currentItem();    // 返回当前item

setCurrentItem();    // 这个不用说也知道是干什么的

setEditTriggers();    // 这是设置何种条件下触发item编辑,可传如下的参数

Constant Value Description
QAbstractItemView::NoEditTriggers 0 No editing possible.
QAbstractItemView::CurrentChanged 1 Editing start whenever current item changes.
QAbstractItemView::DoubleClicked 2 Editing starts when an item is double clicked.
QAbstractItemView::SelectedClicked 4 Editing starts when clicking on an already selected item.
QAbstractItemView::EditKeyPressed 8 Editing starts when the platform edit key has been pressed over an item.
QAbstractItemView::AnyKeyPressed 16 Editing starts when any key is pressed over an item.
QAbstractItemView::AllEditTriggers 31 Editing starts for all above actions.

item(int row, int col);       // 获取指定行列的item

setColumnWidth();          // 设置列宽


QTableWidgetItem

QTreeWidgetItem

QListWidgetItem       以上三个类是具体的项

此三个类常用函数:

childCount();    //  子项的个数

child(int index);    //第index个子项

setFlags (Qt::ItemFlags flags)    // 设置item状态,灰化,是否可编辑等

Qt::ItemFlags取值

Constant Value Description
Qt::NoItemFlags 0 It does not have any properties set.
Qt::ItemIsSelectable 1 It can be selected.
Qt::ItemIsEditable 2 It can be edited.
Qt::ItemIsDragEnabled 4 It can be dragged.
Qt::ItemIsDropEnabled 8 It can be used as a drop target.
Qt::ItemIsUserCheckable 16 It can be checked or unchecked by the user.
Qt::ItemIsEnabled 32 The user can interact with the item.
Qt::ItemIsTristate 64 The item is checkable with three separate states

猜你喜欢

转载自blog.csdn.net/yu1665090104/article/details/79895688