Skip to content

Instantly share code, notes, and snippets.

@dridk
Last active January 3, 2021 11:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dridk/7ed2c4442ed96bacda9151d559a373f4 to your computer and use it in GitHub Desktop.
Save dridk/7ed2c4442ed96bacda9151d559a373f4 to your computer and use it in GitHub Desktop.
PySide2 example using QAbstractListModel
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from PySide2.QtGui import *
import sys
class GameListModel(QAbstractListModel):
"""A model to store a list of things"""
def __init__(self, parent=None):
super().__init__(parent)
self.items = [
{"name": "Tomb Raider", "description": "Action game"},
{"name": "Super Mario", "description": "Aracade game"},
{"name": "Civilization", "description": "Strategie game"},
]
def rowCount(self, parent=QModelIndex()):
""" override : return row count """
return len(self.items)
def data(self, index: QModelIndex(), role: Qt.ItemDataRole):
""" override : Display data according index and role """
# Return none, in index is not valid
if not index.isValid():
return None
item = self.items[index.row()]
if role == Qt.DisplayRole:
return item["name"]
if role == Qt.ToolTipRole:
return item["description"]
if role == Qt.DecorationRole:
return qApp.style().standardIcon(QStyle.SP_DirIcon)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = QListView()
m = GameListModel()
w.setModel(m)
w.show()
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment