PyQt5 Roles and the MVC

Viewed 145

I was trying to code a small ToDo app and stumbled upon lack of understanding of how Model handles Roles, I mean, how does it differentiate one Role from another one? Does it, like, check out all of them at the same time?

I would be glad to quench my curiosity

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)
1 Answers

The roles serve to store and access to a type of information of a certain item. The developers of Qt realized that there is basic information so it established basic roles through Qt.ItemDataRole:

enter image description here

(there are more roles, to see them use the link)

As are the pre-established roles, then default delegates use it to obtain the information to paint the item.

For more information check Model/View Programming

Related