pyqt model/view框架 1.第一个model
>关于Qt的mvc模式就不多说了,网上很多很多,这里按着 Chen Chun-Chia的《PyQt's Model/View Framework》
走一边
我的第一个model
class MyListModel(QAbstractListModel):
"""
我的第一个模型
"""
def __init__(self,parent=None):
super(MyListModel,self).__init__(parent)
#这是数据
self._data=[70,90,20,50]
pass
def rowCount(self, parent=QModelIndex()):
"""
这个方法返回了数据的行数
也就是有多少个条目得数据
"""
return len(self._data)
def data(self,index,role=Qt.DisplayRole):
"""
根据当前index索引,返回当前的数据
然后再由Qt进行渲染显示
"""
#如果当前得索引是不活动得
if not index.isValid() or not 0 <= index.row() < self.rowCount():
#亦或者当前的索引值不在合理范围,即小于等于0,超出总行数
return QVariant() #返回一个QVariant,相当与空条目
#从索引取得当前的航号
row=index.row()
#如果当前角色是DisplayRole
if role==Qt.DisplayRole:
#返回当前行的数据
return self._data[row]
#如果角色不满足需求,则返回QVariant
return QVariant()
代码中,我们覆盖了一个model中最基本得方法:
rowCount
数据总行数;
data
获取数据;
并在init构造方法中,设置好数据源self._data
运行下面的代码试试看:
# -*- coding: utf-8 -*-
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
####################################################################
def main():
app=QApplication(sys.argv)
#新建一个ListView
view=QListView()
#新建一个自定义Model
model=MyListModel()
#设置view的model
view.setModel(model)
view.show()
sys.exit(app.exec_())
####################################################################
class MyListModel(QAbstractListModel):
"""
我的第一个模型
"""
def __init__(self,parent=None):
super(MyListModel,self).__init__(parent)
#这是数据
self._data=[70,90,20,50]
pass
def rowCount(self, parent=QModelIndex()):
"""
这个方法返回了数据的行数
也就是有多少个条目得数据
"""
return len(self._data)
def data(self,index,role=Qt.DisplayRole):
"""
根据当前index索引,返回当前的数据
然后再由Qt进行渲染显示
"""
#如果当前得索引是不活动得
if not index.isValid() or not 0 <= index.row() < self.rowCount():
#亦或者当前的索引值不在合理范围,即小于等于0,超出总行数
return QVariant() #返回一个QVariant,相当与空条目
#从索引取得当前的航号
row=index.row()
#如果当前角色是DisplayRole
if role==Qt.DisplayRole:
#返回当前行的数据
return self._data[row]
#如果角色不满足需求,则返回QVariant
return QVariant()
####################################################################
if __name__ == "__main__":
main()
角色类型(Qt.ItemDataRole)
The general purpose roles (and the associated types) are:
Constant Value Description
Qt.DisplayRole 0 The key data to be rendered in the form of text. (QString)
Qt.DecorationRole 1 The data to be rendered as a decoration in the form of an icon. (QColor, QIcon or QPixmap)
Qt.EditRole 2 The data in a form suitable for editing in an editor. (QString)
Qt.ToolTipRole 3 The data displayed in the item's tooltip. (QString)
Qt.StatusTipRole 4 The data displayed in the status bar. (QString)
Qt.WhatsThisRole 5 The data displayed for the item in "What's This?" mode. (QString)
Qt.SizeHintRole 13 The size hint for the item that will be supplied to views. (QSize)
Roles describing appearance and meta data (with associated types):
Constant Value Description
Qt.FontRole 6 The font used for items rendered with the default delegate. (QFont)
Qt.TextAlignmentRole 7 The alignment of the text for items rendered with the default delegate. (Qt.AlignmentFlag)
Qt.BackgroundRole 8 The background brush used for items rendered with the default delegate. (QBrush)
Qt.BackgroundColorRole 8 This role is obsolete. Use BackgroundRole instead.
Qt.ForegroundRole 9 The foreground brush (text color, typically) used for items rendered with the default delegate. (QBrush)
Qt.TextColorRole 9 This role is obsolete. Use ForegroundRole instead.
Qt.CheckStateRole 10 This role is used to obtain the checked state of an item. (Qt.CheckState)
Qt.InitialSortOrderRole 14 This role is used to obtain the initial sort order of a header view section. (Qt.SortOrder). This role was introduced in Qt 4.8.
Accessibility roles (with associated types):
Constant Value Description
Qt.AccessibleTextRole 11 The text to be used by accessibility extensions and plugins, such as screen readers. (QString)
Qt.AccessibleDescriptionRole 12 A description of the item for accessibility purposes. (QString)
User roles:
Constant Value Description
Qt.UserRole 32 The first role that can be used for application-specific purposes.
最后的Qt.UserRole
是用户自定义Role,如果多个Role,可在他的基础上加数字,比如Qt.UserRole+1
,Qt.UserRole+2
>目前为止,我们使用自定义Model完成了一个最基本的demo,并且知道了Qt.ItemDataRole的各种类型
>
>下一篇,将介绍Qt Model/View 中的委托 Delegate
,通过它我们可以自由渲染view的显示效果
分类:
python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述