首页 > 解决方案 > 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)

标签: pythonmodel-view-controllerpyqtpyqt5

解决方案


角色用于存储和访问特定项目的一类信息。Qt 的开发人员意识到有基本信息,因此它通过以下方式建立了基本角色Qt.ItemDataRole

在此处输入图像描述

(还有更多角色,看他们使用链接)

与预先建立的角色一样,默认代表使用它来获取信息以绘制项目。

有关更多信息,请查看模型/视图编程


推荐阅读