-
Notifications
You must be signed in to change notification settings - Fork 0
/
ListModel.py
59 lines (45 loc) · 1.7 KB
/
ListModel.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import sys
import os
import time
from PySide2 import QtWidgets as qtw
from PySide2 import QtGui as qtg
from PySide2 import QtCore as qtc
from PySide2 import QtQuick as qtq
from PySide2 import QtQml as qtm
class ListModel(qtc.QAbstractListModel):
# Initializes a python list to contain the elements and sets the role
# element_type must subclass QObject and must have a variable "roles"
def __init__(self, element_type, parent=None):
super(ListModel, self).__init__()
self._list = []
self.roles = element_type.roles
def roleNames(self):
return self.roles
# Apppends the item to the list
def appendRow(self, item, parent=qtc.QModelIndex()):
self.beginInsertRows(parent, len(self._list), len(self._list))
self._list.append(item)
self.endInsertRows()
# Clears the list and sets a new list
def setList(self, newList, parent=qtc.QModelIndex()):
self.beginInsertRows(parent, 0, len(self._list)-1)
self._list = newList
self.endInsertRows()
# Clears the list
def clear(self, parent=qtc.QModelIndex()):
self.beginResetModel()
self._list.clear()
self.endResetModel()
# Returns the row count
def rowCount(self, parent=qtc.QModelIndex()):
return len(self._list)
# Returns the data if it fits a given role
def data(self, index, role):
row = index.row()
if index.isValid() and 0 <= row < self.rowCount():
return self._list[index.row()].data(role)
def get(self, index, role):
if(index < len(self._list)):
return self._list[index].data(role)
def __str__(self):
return str(self._list)