Model View Tutorial Examples#

Example code for the Model View Tutorial.

import sys

from PySide6.QtCore import QAbstractTableModel, Qt
from PySide6.QtWidgets import QApplication, QTableView

"""PySide6 port of the widgets/tutorials/modelview/1_readonly example from Qt v6.x"""


#! [1]
class MyModel(QAbstractTableModel):
    def __init__(self, parent=None):
        super().__init__(parent)

    def rowCount(self, parent=None):
        return 2

    def columnCount(self, parent=None):
        return 3

    def data(self, index, role=Qt.DisplayRole):
        if role == Qt.DisplayRole:
            row = index.row() + 1
            column = index.column() + 1
            return f"Row{row}, Column{column}"
        return None
#! [1]


if __name__ == '__main__':
    app = QApplication(sys.argv)
    table_view = QTableView()
    my_model = MyModel()
    table_view.setModel(my_model)
    table_view.show()
    sys.exit(app.exec())
import sys

from PySide6.QtCore import QAbstractTableModel, Qt
from PySide6.QtGui import QBrush, QFont
from PySide6.QtWidgets import QApplication, QTableView

"""PySide6 port of the widgets/tutorials/modelview/2_formatting example from Qt v6.x"""


class MyModel(QAbstractTableModel):
    def __init__(self, parent=None):
        super().__init__(parent)

    def rowCount(self, parent=None):
        return 2

    def columnCount(self, parent=None):
        return 3

#! [1]
    def data(self, index, role=Qt.DisplayRole):
        row = index.row()
        col = index.column()
        # generate a log message when this method gets called
        print(f"row {row}, col{col}, role {role}")

        if role == Qt.DisplayRole:
            if row == 0 and col == 1:
                return "<--left"
            if row == 1 and col == 1:
                return "right-->"
            return f"Row{row}, Column{col+1}"

        elif role == Qt.FontRole:
            if row == 0 and col == 0:  # change font only for cell(0,0)
                bold_font = QFont()
                bold_font.setBold(True)
                return bold_font

        elif role == Qt.BackgroundRole:
            if row == 1 and col == 2:  # change background only for cell(1,2)
                return QBrush(Qt.red)

        elif role == Qt.TextAlignmentRole:
            if row == 1 and col == 1:  # change text alignment only for cell(1,1)
                return Qt.AlignRight | Qt.AlignVCenter

        elif role == Qt.CheckStateRole:
            if row == 1 and col == 0:  # add a checkbox to cell(1,0)
                return Qt.Checked

        return None
#! [1]


if __name__ == '__main__':
    app = QApplication(sys.argv)
    table_view = QTableView()
    my_model = MyModel()
    table_view.setModel(my_model)
    table_view.show()
    sys.exit(app.exec())
import sys

from PySide6.QtCore import QAbstractTableModel, QTime, QTimer, Qt, Slot
from PySide6.QtWidgets import QApplication, QTableView

"""PySide6 port of the widgets/tutorials/modelview/3_changingmodel example from Qt v6.x"""


class MyModel(QAbstractTableModel):
#! [1]
    def __init__(self, parent=None):
        super().__init__(parent)
        self._timer = QTimer(self)
        self._timer.setInterval(1000)
        self._timer.timeout.connect(self.timer_hit)
        self._timer.start()
#! [1]

    def rowCount(self, parent=None):
        return 2

    def columnCount(self, parent=None):
        return 3

#! [2]
    def data(self, index, role=Qt.DisplayRole):
        row = index.row()
        col = index.column()
        if role == Qt.DisplayRole and row == 0 and col == 0:
            return QTime.currentTime().toString()
        return None
#! [2]

#! [3]
    @Slot()
    def timer_hit(self):
        # we identify the top left cell
        top_left = self.createIndex(0, 0)
        # emit a signal to make the view reread identified data
        self.dataChanged.emit(top_left, top_left, [Qt.DisplayRole])
#! [3]

if __name__ == '__main__':
    app = QApplication(sys.argv)
    table_view = QTableView()
    my_model = MyModel()
    table_view.setModel(my_model)
    table_view.show()
    sys.exit(app.exec())
import sys

from PySide6.QtCore import QAbstractTableModel, Qt
from PySide6.QtWidgets import QApplication, QTableView

"""PySide6 port of the widgets/tutorials/modelview/4_headers example from Qt v6.x"""


class MyModel(QAbstractTableModel):
    def __init__(self, parent=None):
        super().__init__(parent)

    def rowCount(self, parent=None):
        return 2

    def columnCount(self, parent=None):
        return 3

    def data(self, index, role=Qt.DisplayRole):
        if role == Qt.DisplayRole:
            row = index.row() + 1
            column = index.column() + 1
            return f"Row{row}, Column{column}"
        return None

#! [1]
    def headerData(self, section, orientation, role):
        if role == Qt.DisplayRole and orientation == Qt.Horizontal:
            return ["first", "second", "third"][section]
        return None
#! [1]


if __name__ == '__main__':
    app = QApplication(sys.argv)
    table_view = QTableView()
    my_model = MyModel()
    table_view.setModel(my_model)
    table_view.show()
    sys.exit(app.exec())
import sys
from itertools import chain

from PySide6.QtCore import QAbstractTableModel, Qt, Signal, Slot
from PySide6.QtWidgets import QApplication, QMainWindow, QTableView

"""PySide6 port of the widgets/tutorials/modelview/5_edit example from Qt v6.x"""


COLS = 3
ROWS = 2


class MyModel(QAbstractTableModel):

    editCompleted = Signal(str)

    def __init__(self, parent=None):
        super().__init__(parent)
        self._grid_data = [["" for y in range(COLS)] for x in range(ROWS)]

    def rowCount(self, parent=None):
        return ROWS

    def columnCount(self, parent=None):
        return COLS

    def data(self, index, role=Qt.DisplayRole):
        if role == Qt.DisplayRole and self.checkIndex(index):
            return self._grid_data[index.row()][index.column()]
        return None

#! [1]
    def setData(self, index, value, role):
        if role != Qt.EditRole or not self.checkIndex(index):
            return False
        # save value from editor to member m_gridData
        self._grid_data[index.row()][index.column()] = value
        # for presentation purposes only: build and emit a joined string
        result = " ".join(chain(*self._grid_data))
        self.editCompleted.emit(result)
        return True
#! [1]

#! [2]
    def flags(self, index):
        return Qt.ItemIsEditable | super().flags(index)
#! [2]


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self._table_view = QTableView(self)
        self.setCentralWidget(self._table_view)
        my_model = MyModel(self)
        self._table_view.setModel(my_model)
        # transfer changes to the model to the window title
        my_model.editCompleted.connect(self.show_window_title)

    @Slot(str)
    def show_window_title(self, title):
        self.setWindowTitle(title)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec())
import sys

from PySide6.QtCore import Qt
from PySide6.QtGui import QStandardItem, QStandardItemModel
from PySide6.QtWidgets import QApplication, QMainWindow, QTreeView

"""PySide6 port of the widgets/tutorials/modelview/6_treeview example from Qt v6.x"""

#! [1]
class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self._standard_model = QStandardItemModel(self)
        self._tree_view = QTreeView(self)
        self.setCentralWidget(self._tree_view)

        prepared_row = self.prepare_row("first", "second", "third")
        item = self._standard_model.invisibleRootItem()
        # adding a row to the invisible root item produces a root element
        item.appendRow(prepared_row)

        second_row = self.prepare_row("111", "222", "333")
        # adding a row to an item starts a subtree
        prepared_row[0].appendRow(second_row)

        self._tree_view.setModel(self._standard_model)
        self._tree_view.expandAll()

    def prepare_row(self, first, second, third):
        return [QStandardItem(first), QStandardItem(second),
                QStandardItem(third)]
#! [1]


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec())
import sys

from PySide6.QtCore import QItemSelection, Qt, Slot
from PySide6.QtGui import QStandardItem, QStandardItemModel
from PySide6.QtWidgets import QApplication, QMainWindow, QTreeView

"""PySide6 port of the widgets/tutorials/modelview/7_selections example from Qt v6.x"""

#! [1]
class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self._standard_model = QStandardItemModel(self)
        self._tree_view = QTreeView(self)
        self.setCentralWidget(self._tree_view)

        # defining a couple of items
        root_node = self._standard_model.invisibleRootItem()

        america_item = QStandardItem("America")
        mexico_item = QStandardItem("Canada")
        usa_item = QStandardItem("USA")
        boston_item = QStandardItem("Boston")
        europe_item = QStandardItem("Europe")
        italy_item = QStandardItem("Italy")
        rome_item = QStandardItem("Rome")
        verona_item = QStandardItem("Verona")

        # building up the hierarchy
        root_node.appendRow(america_item)
        root_node.appendRow(europe_item)
        america_item.appendRow(mexico_item)
        america_item.appendRow(usa_item)
        usa_item.appendRow(boston_item)
        europe_item.appendRow(italy_item)
        italy_item.appendRow(rome_item)
        italy_item.appendRow(verona_item)

        self._tree_view.setModel(self._standard_model)
        self._tree_view.expandAll()

        # selection changes shall trigger a slot
        selection_model = self._tree_view.selectionModel()
        selection_model.selectionChanged.connect(self.selection_changed_slot)
#! [1]

#! [2]
    @Slot(QItemSelection, QItemSelection)
    def selection_changed_slot(self, new_selection, old_selection):
        # get the text of the selected item
        index = self._tree_view.selectionModel().currentIndex()
        selected_text = index.data(Qt.DisplayRole)
        # find out the hierarchy level of the selected item
        hierarchy_level = 1
        seek_root = index
        while seek_root.parent().isValid():
            seek_root = seek_root.parent()
            hierarchy_level += 1
        self.setWindowTitle(f"{selected_text}, Level {hierarchy_level}")
#! [2]


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec())