-
-
Save skriticos/5415869 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python3 | |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# In this prototype/example a QTreeView is created. Then it's populated with | |
# three containers and all containers are populated with three rows, each | |
# containing three columns. | |
# Then the last container is expanded and the last row is selected. | |
# The container items are spanned through the all columns. | |
# Note: this requires > python-3.2 | |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
import sys, os, pprint, time | |
from PySide.QtCore import * | |
from PySide.QtGui import * | |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
app = QApplication(sys.argv) | |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# init widgets | |
view = QTreeView() | |
view.setSelectionBehavior(QAbstractItemView.SelectRows) | |
model = QStandardItemModel() | |
model.setHorizontalHeaderLabels(['col1', 'col2', 'col3']) | |
view.setModel(model) | |
view.setUniformRowHeights(True) | |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# populate data | |
for i in range(3): | |
parent1 = QStandardItem('Family {}. Some long status text for sp'.format(i)) | |
for j in range(3): | |
child1 = QStandardItem('Child {}'.format(i*3+j)) | |
child2 = QStandardItem('row: {}, col: {}'.format(i, j+1)) | |
child3 = QStandardItem('row: {}, col: {}'.format(i, j+2)) | |
parent1.appendRow([child1, child2, child3]) | |
model.appendRow(parent1) | |
# span container columns | |
view.setFirstColumnSpanned(i, view.rootIndex(), True) | |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# expand third container | |
index = model.indexFromItem(parent1) | |
view.expand(index) | |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
# select last row | |
selmod = view.selectionModel() | |
index2 = model.indexFromItem(child3) | |
selmod.select(index2, QItemSelectionModel.Select|QItemSelectionModel.Rows) | |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
view.show() | |
sys.exit(app.exec_()) | |
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
Works fine with Python 2.7.10
I replaced PySide with PyQt4.
Thanks for writing this example, it is one of the best examples using a custom treeView
Would be nice to pack it into a Class like:
class App(QWidget):
...
if name == 'main':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Cool example! The only downside is that PySide and PyQt4 mentioned in the comment above are a bit old by now. I've uploaded a more modern QTreeView example in Python. It's based on PyQt5. Maybe someone will find it useful :-)
It needs some more imports and the QItemSelectionModel is still not found. But when one comments that lines out, it still works.
I do not understand how the rows and columns are counted...:
Family x -> Row x . O.K.
But colum is unclear I would expect:
Row: 0, Col 1 Row: 0 col 2
Row: 0, Col 1 Row: 0 col 2
Row: 0, Col 1 Row: 0 col 2
Row 1: col 1 Row 1 col 2
Row 1: col 1 Row 1 col 2
Row 1: col 1 Row 1 col 2
But The app shows:
Row: 0, Col 1 Row: 0 col 2
Row: 0, Col 2 Row: 0 col 3
Row: 0, Col 3 Row: 0 col 4
???
Example runs fine with Python 2.7.6