Qt有自己的MVC框架,分别是model(模型)、view(视图)、delegate(委托),这篇文章,简单的介绍以下Qt中有关model(模型)的类以及一些基本的使用。@
Qt官方的文档已经很详细了,如果想要详细的去了解,建议花点精力去看官方文档。
QSortFilterProxyModel can be used for sorting items, filtering out items, or both. The model transforms the structure of a source model by mapping the model indexes it supplies to new indexes, corresponding to different locations, for views to use. This approach allows a given source model to be restructured as far as views are concerned without requiring any transformations on the underlying data, and without duplicating the data in memory.以下是基本的用法:
QSortFilterProxyModel 可用于排序项目、过滤项目或两者兼而有之。 该模型通过将其提供的模型索引映射到新索引(对应于不同位置)来转换源模型的结构,以供视图使用。 这种方法允许就视图而言对给定的源模型进行重构,而无需对基础数据进行任何转换,也无需复制内存中的数据。
QIdentityProxyModel can be used to forward the structure of a source model exactly, with no sorting, filtering or other transformation. This is similar in concept to an identity matrix where A.I = A.也就是说,这个model不会对源model进行任何转换,只会简简单单的进行映射。主要是为了使用者可以通过重写data()函数,来定制每一个元素所显示的效果。同时这样也不会污染源model的数据,方便进行重用。也对应这上面的,proxy(代理)的作用就是一个源model可以在许多个view里设置,且基本互不影响。
Because it does no sorting or filtering, this class is most suitable to proxy models which transform the data() of the source model. For example, a proxy model could be created to define the font used, or the background colour, or the tooltip etc. This removes the need to implement all data handling in the same class that creates the structure of the model, and can also be used to create re-usable components.
QIdentityProxyModel可以用于准确地转发源模型的结构,不需要排序、过滤或其他转换。这在概念上类似于单位矩阵,其中A.I = A。
因为它不进行排序或过滤,所以这个类最适合于转换源模型的data()的代理模型。例如,可以创建一个代理模型来定义所使用的字体、背景颜色或工具提示等。这样就不需要在创建模型结构的同一个类中实现所有数据处理,并且还可以用于创建可重用的
QStandardItemModel provides a classic item-based approach to working with the model. The items in a QStandardItemModel are provided by QStandardItem.QStandardItemModel实现了QAbstractItemModel接口,这意味着该模型可以用于在任何支持该接口的视图中提供数据(例如QListView, QTableView和QTreeView,以及您自己的自定义视图)。这是一个基于项的模型,像上面介绍的这些,基本都是特例化的一些子类,QStandardItemModel则可以自己创建一个整体的结构,Table、Tree或者List这些,都可以创建并填充数据。
QStandardItemModel提供了一种经典的基于项的方法来处理模型。QStandardItemModel中的项由QStandardItem提供。
When you want a list or tree, you typically create an empty QStandardItemModel and use appendRow() to add items to the model, and item() to access an item. If your model represents a table, you typically pass the dimensions of the table to the QStandardItemModel constructor and use setItem() to position items into the table. You can also use setRowCount() and setColumnCount() to alter the dimensions of the model. To insert items, use insertRow() or insertColumn(), and to remove items, use removeRow() or removeColumn().以Table结构为例,简单的用法:
You can set the header labels of your model with setHorizontalHeaderLabels() and setVerticalHeaderLabels().
You can search for items in the model with findItems(), and sort the model by calling sort().
Call clear() to remove all items from the model
当您需要列表或树时,您通常创建一个空的QStandardItemModel,并使用appendRow()向模型添加项,并使用item()访问项。如果您的模型表示一个表,您通常将表的维度传递给QStandardItemModel构造函数,并使用setItem()将项目定位到表中。您还可以使用setRowCount()和setColumnCount()来更改模型的维度。要插入项,请使用insertRow()或insertColumn(),要删除项,请使用removeRow()或removeColumn()。您可以使用setHorizontalHeaderLabels()
欢迎光临 IT评测·应用市场-qidao123.com (https://dis.qidao123.com/) | Powered by Discuz! X3.4 |