aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2022-08-08 13:09:06 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2022-08-12 08:35:08 +0200
commit98f393520ed8e905390a0122b572a48439f20663 (patch)
tree5c44542a2749271f17113c8ec0fb45969a7e066d
parente93421ba2b889c383798eb43b7e575f8c7d6eaa1 (diff)
Port the modelview tutorials
Planning to add a mechanism to extract snippets from them. Task-number: PYSIDE-1984 Change-Id: I0277ec27cb1eea85af8a921395374fc30935c99c Reviewed-by: Adrian Herrmann <adrian.herrmann@qt.io> Reviewed-by: Cristian Maureira-Fredes <cristian.maureira-fredes@qt.io> (cherry picked from commit b4fc1c5d81053d30a9b03fe8a0d15336f024ed7a) Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Christian Tismer <tismer@stackless.com>
-rw-r--r--examples/widgets/tutorials/modelview/1_readonly.py73
-rw-r--r--examples/widgets/tutorials/modelview/2_formatting.py100
-rw-r--r--examples/widgets/tutorials/modelview/3_changingmodel.py84
-rw-r--r--examples/widgets/tutorials/modelview/4_headers.py78
-rw-r--r--examples/widgets/tutorials/modelview/5_edit.py106
-rw-r--r--examples/widgets/tutorials/modelview/6_treeview.py78
-rw-r--r--examples/widgets/tutorials/modelview/7_selections.py104
-rw-r--r--examples/widgets/tutorials/modelview/doc/modelview.rst4
-rw-r--r--examples/widgets/tutorials/modelview/modelview.pyproject9
9 files changed, 636 insertions, 0 deletions
diff --git a/examples/widgets/tutorials/modelview/1_readonly.py b/examples/widgets/tutorials/modelview/1_readonly.py
new file mode 100644
index 000000000..96bf04a57
--- /dev/null
+++ b/examples/widgets/tutorials/modelview/1_readonly.py
@@ -0,0 +1,73 @@
+#############################################################################
+##
+## Copyright (C) 2022 The Qt Company Ltd.
+## Contact: https://www.qt.io/licensing/
+##
+## This file is part of the Qt for Python examples of the Qt Toolkit.
+##
+## $QT_BEGIN_LICENSE:BSD$
+## You may use this file under the terms of the BSD license as follows:
+##
+## "Redistribution and use in source and binary forms, with or without
+## modification, are permitted provided that the following conditions are
+## met:
+## * Redistributions of source code must retain the above copyright
+## notice, this list of conditions and the following disclaimer.
+## * Redistributions in binary form must reproduce the above copyright
+## notice, this list of conditions and the following disclaimer in
+## the documentation and/or other materials provided with the
+## distribution.
+## * Neither the name of The Qt Company Ltd nor the names of its
+## contributors may be used to endorse or promote products derived
+## from this software without specific prior written permission.
+##
+##
+## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+##
+## $QT_END_LICENSE$
+##
+#############################################################################
+
+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"""
+
+
+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
+
+
+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())
diff --git a/examples/widgets/tutorials/modelview/2_formatting.py b/examples/widgets/tutorials/modelview/2_formatting.py
new file mode 100644
index 000000000..ac6c7e5dd
--- /dev/null
+++ b/examples/widgets/tutorials/modelview/2_formatting.py
@@ -0,0 +1,100 @@
+#############################################################################
+##
+## Copyright (C) 2022 The Qt Company Ltd.
+## Contact: https://www.qt.io/licensing/
+##
+## This file is part of the Qt for Python examples of the Qt Toolkit.
+##
+## $QT_BEGIN_LICENSE:BSD$
+## You may use this file under the terms of the BSD license as follows:
+##
+## "Redistribution and use in source and binary forms, with or without
+## modification, are permitted provided that the following conditions are
+## met:
+## * Redistributions of source code must retain the above copyright
+## notice, this list of conditions and the following disclaimer.
+## * Redistributions in binary form must reproduce the above copyright
+## notice, this list of conditions and the following disclaimer in
+## the documentation and/or other materials provided with the
+## distribution.
+## * Neither the name of The Qt Company Ltd nor the names of its
+## contributors may be used to endorse or promote products derived
+## from this software without specific prior written permission.
+##
+##
+## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+##
+## $QT_END_LICENSE$
+##
+#############################################################################
+
+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
+
+ 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
+
+
+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())
diff --git a/examples/widgets/tutorials/modelview/3_changingmodel.py b/examples/widgets/tutorials/modelview/3_changingmodel.py
new file mode 100644
index 000000000..aab0e04c6
--- /dev/null
+++ b/examples/widgets/tutorials/modelview/3_changingmodel.py
@@ -0,0 +1,84 @@
+#############################################################################
+##
+## Copyright (C) 2022 The Qt Company Ltd.
+## Contact: https://www.qt.io/licensing/
+##
+## This file is part of the Qt for Python examples of the Qt Toolkit.
+##
+## $QT_BEGIN_LICENSE:BSD$
+## You may use this file under the terms of the BSD license as follows:
+##
+## "Redistribution and use in source and binary forms, with or without
+## modification, are permitted provided that the following conditions are
+## met:
+## * Redistributions of source code must retain the above copyright
+## notice, this list of conditions and the following disclaimer.
+## * Redistributions in binary form must reproduce the above copyright
+## notice, this list of conditions and the following disclaimer in
+## the documentation and/or other materials provided with the
+## distribution.
+## * Neither the name of The Qt Company Ltd nor the names of its
+## contributors may be used to endorse or promote products derived
+## from this software without specific prior written permission.
+##
+##
+## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+##
+## $QT_END_LICENSE$
+##
+#############################################################################
+
+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):
+ 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()
+
+ def rowCount(self, parent=None):
+ return 2
+
+ def columnCount(self, parent=None):
+ return 3
+
+ 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
+
+ @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])
+
+
+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())
diff --git a/examples/widgets/tutorials/modelview/4_headers.py b/examples/widgets/tutorials/modelview/4_headers.py
new file mode 100644
index 000000000..0a2f05feb
--- /dev/null
+++ b/examples/widgets/tutorials/modelview/4_headers.py
@@ -0,0 +1,78 @@
+#############################################################################
+##
+## Copyright (C) 2022 The Qt Company Ltd.
+## Contact: https://www.qt.io/licensing/
+##
+## This file is part of the Qt for Python examples of the Qt Toolkit.
+##
+## $QT_BEGIN_LICENSE:BSD$
+## You may use this file under the terms of the BSD license as follows:
+##
+## "Redistribution and use in source and binary forms, with or without
+## modification, are permitted provided that the following conditions are
+## met:
+## * Redistributions of source code must retain the above copyright
+## notice, this list of conditions and the following disclaimer.
+## * Redistributions in binary form must reproduce the above copyright
+## notice, this list of conditions and the following disclaimer in
+## the documentation and/or other materials provided with the
+## distribution.
+## * Neither the name of The Qt Company Ltd nor the names of its
+## contributors may be used to endorse or promote products derived
+## from this software without specific prior written permission.
+##
+##
+## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+##
+## $QT_END_LICENSE$
+##
+#############################################################################
+
+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
+
+ def headerData(self, section, orientation, role):
+ if role == Qt.DisplayRole and orientation == Qt.Horizontal:
+ return ["first", "second", "third"][section]
+ return None
+
+
+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())
diff --git a/examples/widgets/tutorials/modelview/5_edit.py b/examples/widgets/tutorials/modelview/5_edit.py
new file mode 100644
index 000000000..b43876cbc
--- /dev/null
+++ b/examples/widgets/tutorials/modelview/5_edit.py
@@ -0,0 +1,106 @@
+#############################################################################
+##
+## Copyright (C) 2022 The Qt Company Ltd.
+## Contact: https://www.qt.io/licensing/
+##
+## This file is part of the Qt for Python examples of the Qt Toolkit.
+##
+## $QT_BEGIN_LICENSE:BSD$
+## You may use this file under the terms of the BSD license as follows:
+##
+## "Redistribution and use in source and binary forms, with or without
+## modification, are permitted provided that the following conditions are
+## met:
+## * Redistributions of source code must retain the above copyright
+## notice, this list of conditions and the following disclaimer.
+## * Redistributions in binary form must reproduce the above copyright
+## notice, this list of conditions and the following disclaimer in
+## the documentation and/or other materials provided with the
+## distribution.
+## * Neither the name of The Qt Company Ltd nor the names of its
+## contributors may be used to endorse or promote products derived
+## from this software without specific prior written permission.
+##
+##
+## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+##
+## $QT_END_LICENSE$
+##
+#############################################################################
+
+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
+
+ 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
+
+ def flags(self, index):
+ return Qt.ItemIsEditable | super().flags(index)
+
+
+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())
diff --git a/examples/widgets/tutorials/modelview/6_treeview.py b/examples/widgets/tutorials/modelview/6_treeview.py
new file mode 100644
index 000000000..5161a9550
--- /dev/null
+++ b/examples/widgets/tutorials/modelview/6_treeview.py
@@ -0,0 +1,78 @@
+#############################################################################
+##
+## Copyright (C) 2022 The Qt Company Ltd.
+## Contact: https://www.qt.io/licensing/
+##
+## This file is part of the Qt for Python examples of the Qt Toolkit.
+##
+## $QT_BEGIN_LICENSE:BSD$
+## You may use this file under the terms of the BSD license as follows:
+##
+## "Redistribution and use in source and binary forms, with or without
+## modification, are permitted provided that the following conditions are
+## met:
+## * Redistributions of source code must retain the above copyright
+## notice, this list of conditions and the following disclaimer.
+## * Redistributions in binary form must reproduce the above copyright
+## notice, this list of conditions and the following disclaimer in
+## the documentation and/or other materials provided with the
+## distribution.
+## * Neither the name of The Qt Company Ltd nor the names of its
+## contributors may be used to endorse or promote products derived
+## from this software without specific prior written permission.
+##
+##
+## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+##
+## $QT_END_LICENSE$
+##
+#############################################################################
+
+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"""
+
+
+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)]
+
+
+if __name__ == '__main__':
+ app = QApplication(sys.argv)
+ w = MainWindow()
+ w.show()
+ sys.exit(app.exec())
diff --git a/examples/widgets/tutorials/modelview/7_selections.py b/examples/widgets/tutorials/modelview/7_selections.py
new file mode 100644
index 000000000..094d2f4bf
--- /dev/null
+++ b/examples/widgets/tutorials/modelview/7_selections.py
@@ -0,0 +1,104 @@
+#############################################################################
+##
+## Copyright (C) 2022 The Qt Company Ltd.
+## Contact: https://www.qt.io/licensing/
+##
+## This file is part of the Qt for Python examples of the Qt Toolkit.
+##
+## $QT_BEGIN_LICENSE:BSD$
+## You may use this file under the terms of the BSD license as follows:
+##
+## "Redistribution and use in source and binary forms, with or without
+## modification, are permitted provided that the following conditions are
+## met:
+## * Redistributions of source code must retain the above copyright
+## notice, this list of conditions and the following disclaimer.
+## * Redistributions in binary form must reproduce the above copyright
+## notice, this list of conditions and the following disclaimer in
+## the documentation and/or other materials provided with the
+## distribution.
+## * Neither the name of The Qt Company Ltd nor the names of its
+## contributors may be used to endorse or promote products derived
+## from this software without specific prior written permission.
+##
+##
+## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+##
+## $QT_END_LICENSE$
+##
+#############################################################################
+
+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"""
+
+
+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)
+
+ @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}")
+
+
+if __name__ == '__main__':
+ app = QApplication(sys.argv)
+ w = MainWindow()
+ w.show()
+ sys.exit(app.exec())
diff --git a/examples/widgets/tutorials/modelview/doc/modelview.rst b/examples/widgets/tutorials/modelview/doc/modelview.rst
new file mode 100644
index 000000000..017f78de1
--- /dev/null
+++ b/examples/widgets/tutorials/modelview/doc/modelview.rst
@@ -0,0 +1,4 @@
+Model View Tutorial Examples
+============================
+
+Example code for the Model View Tutorial.
diff --git a/examples/widgets/tutorials/modelview/modelview.pyproject b/examples/widgets/tutorials/modelview/modelview.pyproject
new file mode 100644
index 000000000..3bbe9d47a
--- /dev/null
+++ b/examples/widgets/tutorials/modelview/modelview.pyproject
@@ -0,0 +1,9 @@
+{
+ "files": ["1_readonly.py",
+ "2_formatting.py",
+ "3_changingmodel.py",
+ "4_headers.py",
+ "5_edit.py",
+ "6_treeview.py",
+ "7_selections.py"]
+}