python - PyQt5 角色和 MVC
问题描述
我正在尝试编写一个小型 ToDo 应用程序并偶然发现对模型如何处理角色缺乏了解,我的意思是,它如何区分一个角色和另一个角色?它会像同时检查所有这些吗?
我很乐意熄灭我的好奇心
class TodoModel(QtCore.QAbstractListModel):
""" Model for handling data"""
def __init__(self, todos=None, *args, **kwargs):
super(TodoModel, self).__init__(*args, **kwargs)
self.todos = todos or []
def data(self, index, role):
if role == Qt.DisplayRole: #That Line
status, text = self.todos[index.row()]
return text
if role == Qt.DecorationRole: #And This One
status, text = self.todos[index.row()]
if status:
return tick
def rowCount(self, index):
return len(self.todos)
解决方案
角色用于存储和访问特定项目的一类信息。Qt 的开发人员意识到有基本信息,因此它通过以下方式建立了基本角色Qt.ItemDataRole
:
(还有更多角色,看他们使用链接)
与预先建立的角色一样,默认代表使用它来获取信息以绘制项目。
有关更多信息,请查看模型/视图编程
推荐阅读
- java - 更改标签颜色的 SWT 崩溃
- javascript - 为什么我从代码中得到不同的结果?
- unity3d - 碰撞统一后字符错误动作
- firebase - Stream/Bloc/Repository/Firebase 数据流 Flutter
- javascript - 我的叠加层将页面的其余部分向下推,而不是出现在顶部
- robotframework - Robotframework,AutoIt:错误消息“未找到名称为‘发送’的关键字”
- sql - 在 SELECT 中包含聚合函数结果
- python - 如何使 Scrapy 管道以并行方式处理项目
- reactjs - 在 web3.eth.getAccounts() 中获取一个空数组
- mod-security - 同时使用 owasp CRS 和 Comodo modsecurity 规则集